Skip to content

Issue 467 #468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/common/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
* the base error class
*/
class AppError extends Error {
constructor(instanceId, status, message) {
constructor(instanceId, status, message, lineNo) {
super();

this.instanceId = instanceId;
this.status = status;
this.message = message || 'unknown exception';
this.lineNo = -1;
this.lineNo = lineNo || -1;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class ImportDataDialogComponent implements OnInit {
public instanceId = '';

public rawContent = '';
public flushDB = true;
public flushDB = false;
public opType = '';
public exportType = 'redis';

Expand Down Expand Up @@ -114,18 +114,12 @@ export class ImportDataDialogComponent implements OnInit {

const totalRow = this.flushDB ? commands.length - 1 : commands.length;
this.redisService.call(this.instanceId, commands).subscribe((rsp) => {
let numberOfSucceed = 0;
_.each(rsp, v => {
numberOfSucceed += (!!v && v.toString().toLowerCase().indexOf('err') < 0) ? 1 : 0;
});
numberOfSucceed -= this.flushDB ? 1 : 0;
const numberOfFailed = totalRow - numberOfSucceed;
this.util.showMessage(`${numberOfSucceed} row${numberOfSucceed !== 1 ? 's were' : ' was'} imported
successfully, ${numberOfFailed} row${numberOfFailed !== 1 ? 's have' : ' has'} failed.`);
this.util.showMessage(`All rows were imported successfully.`);
this.dialogRef.close();
this._store.dispatch(new ReqFetchTree({id: this.instanceId}));
}, err => {
this.util.showMessage('Failed to import commands: ' + this.util.getErrorMessage(err));
const errorLineNo = err.error.line - (this.flushDB ? 1 : 0);
this.util.showMessage(`The command on row ${errorLineNo} failed with error: ${this.util.getErrorMessage(err)}`);
});
}
}
5 changes: 0 additions & 5 deletions src/app/ngrx/effects/page-effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {RedisService} from '../../services/redis.service';


import {PageActions, LoadedPage, ReqLoadRootPage} from '../actions/page-actions';
import {RedisConnectFailed} from '../actions/redis-actions';

import {catchError, map, mergeMap} from 'rxjs/operators';
import {Injectable} from '@angular/core';
Expand Down Expand Up @@ -54,10 +53,6 @@ export class PageEffect {
requestId: action['payload'].requestId,
id: action['payload'].id
});
}),
catchError(() => {
const id = action['payload'].id;
return of( new RedisConnectFailed({id}));
})
);
}
Expand Down
19 changes: 1 addition & 18 deletions src/app/ngrx/effects/redis-effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {of, Observable} from 'rxjs';
import {RedisService} from '../../services/redis.service';


import {RedisActions, FetchedTree, RedisConnect, RedisConnectFailed, ReqFetchTree, ReqRedisConnect} from '../actions/redis-actions';
import {RedisActions, FetchedTree, RedisConnect, ReqFetchTree, ReqRedisConnect} from '../actions/redis-actions';
import {catchError, map, mergeMap} from 'rxjs/operators';
import {Injectable} from '@angular/core';
import {Action} from '@ngrx/store';
Expand All @@ -24,7 +24,6 @@ export class RedisEffect {
/**
* send connect request to backend when dispatch "ReqRedisConnect"
* and when backend returned, dispatch data to "RedisConnect"
* when backend return error, dispatch to "RedisConnectFailed"
*/
@Effect()
connectRedis: Observable<Action> = this.actions$.pipe(
Expand All @@ -36,18 +35,6 @@ export class RedisEffect {
action['payload'].scb(data);
}
return new RedisConnect(data);
}),
catchError(() => {
if (action['payload'].fcb) {
action['payload'].fcb(action['payload'].instance);
}
if (action['payload'].instance) {
const id = action['payload'].instance.id;
const host = action['payload'].instance.serverModel.name;
const port = action['payload'].instance.serverModel.port;
this.util.showMessage(`Fail to connect Redis server at ${host}:${port}.`);
return of(new RedisConnectFailed({id}));
}
})
);
}
Expand All @@ -57,7 +44,6 @@ export class RedisEffect {
/**
* send fetch tree request to backend when dispatch "ReqFetchTree"
* and when backend returned, dispatch data to "FetchedTree"
* when backend return error, dispatch to "RedisConnectFailed"
*/
@Effect()
fetchTree: Observable<Action> = this.actions$.pipe(
Expand All @@ -70,9 +56,6 @@ export class RedisEffect {
action['payload'].scb(data);
}
return new FetchedTree({id, data});
}),
catchError(() => {
return of( new RedisConnectFailed({id}));
})
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/app/ngrx/reducer/redis-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {RedisActions} from '../actions/redis-actions';
import {RedisInstance} from '../../models/redis-instance';
import _ from 'lodash';

const REDIS_INSTANCES_KEY = 'REDIS_INSTANCES_KEY';
export const REDIS_INSTANCES_KEY = 'REDIS_INSTANCES_KEY';
const defaultState = [{serverModel: {name: 'default-local', ip: 'localhost', port: 6379, db: 0, password: ''}, id: uuid()}];
let initState = defaultState;

Expand Down Expand Up @@ -75,6 +75,8 @@ export function reducer(state = initialState, action) {
const i = getInstanceById(action.payload.id, state);
i.status = 'failed';
i.working = false;
i.selected = false;
i.expanded = false;
return state;
}
case RedisActions.RedisConnect: {
Expand Down
37 changes: 33 additions & 4 deletions src/app/services/http-helper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import {HttpClient, HttpHeaders} from '@angular/common/http';
import * as _ from 'lodash';
import {environment} from '../../environments/environment';
import {catchError, delay} from 'rxjs/operators';
import {Store} from '@ngrx/store';
import {MatDialog} from '@angular/material';

import {RedisConnectFailed} from '../ngrx/actions/redis-actions';
import {CollapseCli} from '../ngrx/actions/cli-actions';
import {REDIS_INSTANCES_KEY} from '../ngrx/reducer/redis-reducer';
import {UtilService} from './util.service';

export const API_BASE_URL = environment.URI;

Expand All @@ -12,8 +19,12 @@ export const API_BASE_URL = environment.URI;
})

export class HttpHelperService {
constructor(private http: HttpClient) {
}
constructor(
private http: HttpClient,
private util: UtilService,
private _store: Store<any>,
private dialogService: MatDialog
) { }

/**
* Performs a request with `get` http method.
Expand Down Expand Up @@ -45,8 +56,26 @@ export class HttpHelperService {
* catches the auth error
* @param error the error response
*/
catchError(error: Response): Observable<Response> {
return throwError(error);
catchError(error: any): Observable<any> {
const err = error.error;
if (err.code === 500) {
const instancesString = localStorage.getItem(REDIS_INSTANCES_KEY);
const instances = instancesString ? JSON.parse(instancesString) : [];
const instance = _.find(instances, {'id': err.instanceId});
if (instance) {
const id = instance.id;
const host = instance.serverModel.name;
const port = instance.serverModel.port;
// reset UI if redis connection fails
this.dialogService.closeAll();
this.util.showMessage(`Failed to connect Redis server at ${host}:${port}.`);
this._store.dispatch(new RedisConnectFailed({id}));
this._store.dispatch(new CollapseCli());
}
return of();
} else {
return throwError(error);
}
}

/**
Expand Down