ing
This commit is contained in:
parent
8c93055ba1
commit
e1bc9555ec
32
src/packages/commons/service/error-response.ts
Normal file
32
src/packages/commons/service/error-response.ts
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
import { HttpErrorResponse } from '@angular/common/http';
|
||||||
|
|
||||||
|
export class ErrorResponse {
|
||||||
|
|
||||||
|
private constructor(
|
||||||
|
private _code: number,
|
||||||
|
private _exception: string,
|
||||||
|
private _message: string,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public get code(): number {
|
||||||
|
return this._code;
|
||||||
|
}
|
||||||
|
public get exception(): string {
|
||||||
|
return this._exception;
|
||||||
|
}
|
||||||
|
public get message(): string {
|
||||||
|
return this._message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static restError(errorResponse: HttpErrorResponse): ErrorResponse {
|
||||||
|
const aryMsg = errorResponse.error.message.split('|');
|
||||||
|
const resError: ErrorResponse = new ErrorResponse(
|
||||||
|
errorResponse.error.code,
|
||||||
|
aryMsg[0],
|
||||||
|
aryMsg[1] === 'null' ? '' : aryMsg[1],
|
||||||
|
);
|
||||||
|
return resError;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -9,6 +9,8 @@ import 'rxjs/add/operator/do';
|
||||||
import 'rxjs/add/operator/timeout';
|
import 'rxjs/add/operator/timeout';
|
||||||
import 'rxjs/add/observable/throw';
|
import 'rxjs/add/observable/throw';
|
||||||
|
|
||||||
|
import { ErrorResponse } from './error-response';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RESTService {
|
export class RESTService {
|
||||||
private readonly httpHeaders: HttpHeaders;
|
private readonly httpHeaders: HttpHeaders;
|
||||||
|
@ -36,7 +38,7 @@ export class RESTService {
|
||||||
responseType: 'json',
|
responseType: 'json',
|
||||||
})
|
})
|
||||||
.map(response => response)
|
.map(response => response)
|
||||||
.catch((error: HttpErrorResponse) => Observable.throw(error.error));
|
.catch((error: HttpErrorResponse) => Observable.throw(ErrorResponse.restError(error)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public post<T>(entry: string, body: any | null, params?: {[param: string]: string | string[]}): Observable<T> {
|
public post<T>(entry: string, body: any | null, params?: {[param: string]: string | string[]}): Observable<T> {
|
||||||
|
@ -49,7 +51,7 @@ export class RESTService {
|
||||||
responseType: 'json',
|
responseType: 'json',
|
||||||
})
|
})
|
||||||
.map(response => response)
|
.map(response => response)
|
||||||
.catch((error: HttpErrorResponse) => Observable.throw(error.error));
|
.catch((error: HttpErrorResponse) => Observable.throw(ErrorResponse.restError(error)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,6 @@ export class MemberService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public signup(member: Member): Observable<Member> {
|
public signup(member: Member): Observable<Member> {
|
||||||
return this.restService.post('/account/signup', '');
|
return this.restService.post('/account/signup', member);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import { Action } from '@ngrx/store';
|
import { Action } from '@ngrx/store';
|
||||||
|
|
||||||
|
import { ErrorResponse } from 'packages/commons/service/error-response';
|
||||||
|
|
||||||
import { Member } from '../../model/member';
|
import { Member } from '../../model/member';
|
||||||
|
|
||||||
export enum ActionType {
|
export enum ActionType {
|
||||||
|
@ -27,7 +29,7 @@ export class SigninSuccess implements Action {
|
||||||
export class SigninFailure implements Action {
|
export class SigninFailure implements Action {
|
||||||
readonly type = ActionType.SigninFailure;
|
readonly type = ActionType.SigninFailure;
|
||||||
|
|
||||||
constructor(public payload: any) {}
|
constructor(public payload: ErrorResponse) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SigninRedirect implements Action {
|
export class SigninRedirect implements Action {
|
||||||
|
@ -45,7 +47,7 @@ export class SignoutSuccess implements Action {
|
||||||
export class SignoutFailure implements Action {
|
export class SignoutFailure implements Action {
|
||||||
readonly type = ActionType.SignoutFailure;
|
readonly type = ActionType.SignoutFailure;
|
||||||
|
|
||||||
constructor(public payload: any) {}
|
constructor(public payload: ErrorResponse) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Actions =
|
export type Actions =
|
||||||
|
|
|
@ -14,6 +14,8 @@ import 'rxjs/add/operator/switchMap';
|
||||||
import 'rxjs/add/operator/map';
|
import 'rxjs/add/operator/map';
|
||||||
import 'rxjs/add/operator/take';
|
import 'rxjs/add/operator/take';
|
||||||
|
|
||||||
|
import { ErrorResponse } from 'packages/commons/service/error-response';
|
||||||
|
|
||||||
import { Member } from '../../model/member';
|
import { Member } from '../../model/member';
|
||||||
import { MemberService } from '../../service/member.service';
|
import { MemberService } from '../../service/member.service';
|
||||||
|
|
||||||
|
@ -41,7 +43,7 @@ export class Effects {
|
||||||
.map(member => {
|
.map(member => {
|
||||||
return new SigninSuccess(member);
|
return new SigninSuccess(member);
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error: ErrorResponse) => {
|
||||||
return of(new SigninFailure(error));
|
return of(new SigninFailure(error));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { ErrorResponse } from 'packages/commons/service/error-response';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Actions,
|
Actions,
|
||||||
ActionType,
|
ActionType,
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
|
import { ErrorResponse } from 'packages/commons/service/error-response';
|
||||||
|
|
||||||
import { Member } from '../../model/member';
|
import { Member } from '../../model/member';
|
||||||
|
|
||||||
export interface State {
|
export interface State {
|
||||||
isSignin: boolean;
|
isSignin: boolean;
|
||||||
error: string | null;
|
error: ErrorResponse | null;
|
||||||
isPending: boolean;
|
isPending: boolean;
|
||||||
member: Member | null;
|
member: Member | null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import { Action } from '@ngrx/store';
|
import { Action } from '@ngrx/store';
|
||||||
|
|
||||||
|
import { ErrorResponse } from 'packages/commons/service/error-response';
|
||||||
|
|
||||||
import { Member } from '../../model/member';
|
import { Member } from '../../model/member';
|
||||||
|
|
||||||
export enum ActionType {
|
export enum ActionType {
|
||||||
|
@ -23,7 +25,7 @@ export class SignupSuccess implements Action {
|
||||||
export class SignupFailure implements Action {
|
export class SignupFailure implements Action {
|
||||||
readonly type = ActionType.SignupFailure;
|
readonly type = ActionType.SignupFailure;
|
||||||
|
|
||||||
constructor(public payload: any) {}
|
constructor(public payload: ErrorResponse) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Actions =
|
export type Actions =
|
||||||
|
|
|
@ -13,6 +13,8 @@ import 'rxjs/add/operator/exhaustMap';
|
||||||
import 'rxjs/add/operator/map';
|
import 'rxjs/add/operator/map';
|
||||||
import 'rxjs/add/operator/take';
|
import 'rxjs/add/operator/take';
|
||||||
|
|
||||||
|
import { ErrorResponse } from 'packages/commons/service/error-response';
|
||||||
|
|
||||||
import { Member } from '../../model/member';
|
import { Member } from '../../model/member';
|
||||||
import { MemberService } from '../../service/member.service';
|
import { MemberService } from '../../service/member.service';
|
||||||
|
|
||||||
|
@ -43,9 +45,9 @@ export class Effects {
|
||||||
.catch(error => of(new SignupFailure(error)))
|
.catch(error => of(new SignupFailure(error)))
|
||||||
);
|
);
|
||||||
|
|
||||||
@Effect({ dispatch: false })
|
@Effect({ dispatch: false })
|
||||||
signupSuccess$ = this.actions$
|
signupSuccess$ = this.actions$
|
||||||
.ofType(ActionType.SignupSuccess)
|
.ofType(ActionType.SignupSuccess)
|
||||||
.do(() => this.router.navigate(['/']));
|
.do(() => this.router.navigate(['/']));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
import { ErrorResponse } from 'packages/commons/service/error-response';
|
||||||
|
|
||||||
import { Member } from '../../model/member';
|
import { Member } from '../../model/member';
|
||||||
|
|
||||||
export interface State {
|
export interface State {
|
||||||
error: string | null;
|
error: ErrorResponse | null;
|
||||||
isPending: boolean;
|
isPending: boolean;
|
||||||
member: Member | null;
|
member: Member | null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user