From e1bc9555ec64f278a39716bab592b00a4e604f8e Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 6 Mar 2018 12:24:16 +0900 Subject: [PATCH] ing --- .../commons/service/error-response.ts | 32 +++++++++++++++++++ src/packages/commons/service/rest.service.ts | 6 ++-- src/packages/member/service/member.service.ts | 2 +- src/packages/member/store/auth/auth.action.ts | 6 ++-- src/packages/member/store/auth/auth.effect.ts | 4 ++- .../member/store/auth/auth.reducer.ts | 2 ++ src/packages/member/store/auth/auth.state.ts | 4 ++- .../member/store/signup/signup.action.ts | 4 ++- .../member/store/signup/signup.effect.ts | 10 +++--- .../member/store/signup/signup.state.ts | 4 ++- 10 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 src/packages/commons/service/error-response.ts diff --git a/src/packages/commons/service/error-response.ts b/src/packages/commons/service/error-response.ts new file mode 100644 index 0000000..0692c76 --- /dev/null +++ b/src/packages/commons/service/error-response.ts @@ -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; + } +} + diff --git a/src/packages/commons/service/rest.service.ts b/src/packages/commons/service/rest.service.ts index 97beb59..031c8a2 100644 --- a/src/packages/commons/service/rest.service.ts +++ b/src/packages/commons/service/rest.service.ts @@ -9,6 +9,8 @@ import 'rxjs/add/operator/do'; import 'rxjs/add/operator/timeout'; import 'rxjs/add/observable/throw'; +import { ErrorResponse } from './error-response'; + @Injectable() export class RESTService { private readonly httpHeaders: HttpHeaders; @@ -36,7 +38,7 @@ export class RESTService { responseType: 'json', }) .map(response => response) - .catch((error: HttpErrorResponse) => Observable.throw(error.error)); + .catch((error: HttpErrorResponse) => Observable.throw(ErrorResponse.restError(error))); } public post(entry: string, body: any | null, params?: {[param: string]: string | string[]}): Observable { @@ -49,7 +51,7 @@ export class RESTService { responseType: 'json', }) .map(response => response) - .catch((error: HttpErrorResponse) => Observable.throw(error.error)); + .catch((error: HttpErrorResponse) => Observable.throw(ErrorResponse.restError(error))); } } diff --git a/src/packages/member/service/member.service.ts b/src/packages/member/service/member.service.ts index ab25d5b..548472c 100644 --- a/src/packages/member/service/member.service.ts +++ b/src/packages/member/service/member.service.ts @@ -26,6 +26,6 @@ export class MemberService { } public signup(member: Member): Observable { - return this.restService.post('/account/signup', ''); + return this.restService.post('/account/signup', member); } } diff --git a/src/packages/member/store/auth/auth.action.ts b/src/packages/member/store/auth/auth.action.ts index 4697023..dfaee4f 100644 --- a/src/packages/member/store/auth/auth.action.ts +++ b/src/packages/member/store/auth/auth.action.ts @@ -1,5 +1,7 @@ import { Action } from '@ngrx/store'; +import { ErrorResponse } from 'packages/commons/service/error-response'; + import { Member } from '../../model/member'; export enum ActionType { @@ -27,7 +29,7 @@ export class SigninSuccess implements Action { export class SigninFailure implements Action { readonly type = ActionType.SigninFailure; - constructor(public payload: any) {} + constructor(public payload: ErrorResponse) {} } export class SigninRedirect implements Action { @@ -45,7 +47,7 @@ export class SignoutSuccess implements Action { export class SignoutFailure implements Action { readonly type = ActionType.SignoutFailure; - constructor(public payload: any) {} + constructor(public payload: ErrorResponse) {} } export type Actions = diff --git a/src/packages/member/store/auth/auth.effect.ts b/src/packages/member/store/auth/auth.effect.ts index 3315d9c..071c289 100644 --- a/src/packages/member/store/auth/auth.effect.ts +++ b/src/packages/member/store/auth/auth.effect.ts @@ -14,6 +14,8 @@ import 'rxjs/add/operator/switchMap'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/take'; +import { ErrorResponse } from 'packages/commons/service/error-response'; + import { Member } from '../../model/member'; import { MemberService } from '../../service/member.service'; @@ -41,7 +43,7 @@ export class Effects { .map(member => { return new SigninSuccess(member); }) - .catch((error) => { + .catch((error: ErrorResponse) => { return of(new SigninFailure(error)); }); diff --git a/src/packages/member/store/auth/auth.reducer.ts b/src/packages/member/store/auth/auth.reducer.ts index fd4c8a6..eadc120 100644 --- a/src/packages/member/store/auth/auth.reducer.ts +++ b/src/packages/member/store/auth/auth.reducer.ts @@ -1,3 +1,5 @@ +import { ErrorResponse } from 'packages/commons/service/error-response'; + import { Actions, ActionType, diff --git a/src/packages/member/store/auth/auth.state.ts b/src/packages/member/store/auth/auth.state.ts index 87d9f46..c62f990 100644 --- a/src/packages/member/store/auth/auth.state.ts +++ b/src/packages/member/store/auth/auth.state.ts @@ -1,8 +1,10 @@ +import { ErrorResponse } from 'packages/commons/service/error-response'; + import { Member } from '../../model/member'; export interface State { isSignin: boolean; - error: string | null; + error: ErrorResponse | null; isPending: boolean; member: Member | null; } diff --git a/src/packages/member/store/signup/signup.action.ts b/src/packages/member/store/signup/signup.action.ts index a75e475..6f7f814 100644 --- a/src/packages/member/store/signup/signup.action.ts +++ b/src/packages/member/store/signup/signup.action.ts @@ -1,5 +1,7 @@ import { Action } from '@ngrx/store'; +import { ErrorResponse } from 'packages/commons/service/error-response'; + import { Member } from '../../model/member'; export enum ActionType { @@ -23,7 +25,7 @@ export class SignupSuccess implements Action { export class SignupFailure implements Action { readonly type = ActionType.SignupFailure; - constructor(public payload: any) {} + constructor(public payload: ErrorResponse) {} } export type Actions = diff --git a/src/packages/member/store/signup/signup.effect.ts b/src/packages/member/store/signup/signup.effect.ts index 5fe4516..77e8a04 100644 --- a/src/packages/member/store/signup/signup.effect.ts +++ b/src/packages/member/store/signup/signup.effect.ts @@ -13,6 +13,8 @@ import 'rxjs/add/operator/exhaustMap'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/take'; +import { ErrorResponse } from 'packages/commons/service/error-response'; + import { Member } from '../../model/member'; import { MemberService } from '../../service/member.service'; @@ -43,9 +45,9 @@ export class Effects { .catch(error => of(new SignupFailure(error))) ); - @Effect({ dispatch: false }) - signupSuccess$ = this.actions$ - .ofType(ActionType.SignupSuccess) - .do(() => this.router.navigate(['/'])); + @Effect({ dispatch: false }) + signupSuccess$ = this.actions$ + .ofType(ActionType.SignupSuccess) + .do(() => this.router.navigate(['/'])); } diff --git a/src/packages/member/store/signup/signup.state.ts b/src/packages/member/store/signup/signup.state.ts index ae2660b..bf94f2d 100644 --- a/src/packages/member/store/signup/signup.state.ts +++ b/src/packages/member/store/signup/signup.state.ts @@ -1,7 +1,9 @@ +import { ErrorResponse } from 'packages/commons/service/error-response'; + import { Member } from '../../model/member'; export interface State { - error: string | null; + error: ErrorResponse | null; isPending: boolean; member: Member | null; }