feat: add error response code condition for error title

resolves #2763
pull/2769/head
mehmet-erim 5 years ago
parent 7e97483d28
commit 55f30c51cb

@ -57,97 +57,108 @@ export class ErrorHandler {
private injector: Injector,
@Inject('HTTP_ERROR_CONFIG') private httpErrorConfig: HttpErrorConfig,
) {
this.actions.pipe(ofActionSuccessful(RestOccurError, RouterError, RouterDataResolved)).subscribe(res => {
if (res instanceof RestOccurError) {
const { payload: err = {} as HttpErrorResponse | any } = res;
const body = snq(() => (err as HttpErrorResponse).error.error, DEFAULT_ERROR_MESSAGES.defaultError.title);
if (err instanceof HttpErrorResponse && err.headers.get('_AbpErrorFormat')) {
const confirmation$ = this.showError(null, null, body);
if (err.status === 401) {
confirmation$.subscribe(() => {
this.navigateToLogin();
});
}
} else {
switch ((err as HttpErrorResponse).status) {
case 401:
this.canCreateCustomError(401)
? this.show401Page()
: this.showError(
{
key: 'AbpAccount::DefaultErrorMessage401',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.title,
},
{
key: 'AbpAccount::DefaultErrorMessage401Detail',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.details,
},
).subscribe(() => this.navigateToLogin());
break;
case 403:
this.createErrorComponent({
title: {
key: 'AbpAccount::DefaultErrorMessage403',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.title,
},
details: {
key: 'AbpAccount::DefaultErrorMessage403Detail',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.details,
},
status: 403,
});
break;
case 404:
this.canCreateCustomError(404)
? this.show404Page()
: this.showError(
{
key: 'AbpAccount::DefaultErrorMessage404',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.details,
},
{
key: 'AbpAccount::DefaultErrorMessage404Detail',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.title,
},
);
break;
case 500:
this.createErrorComponent({
title: {
key: 'AbpAccount::500Message',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.title,
},
details: {
key: 'AbpAccount::InternalServerErrorMessage',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.details,
},
status: 500,
this.actions
.pipe(ofActionSuccessful(RestOccurError, RouterError, RouterDataResolved))
.subscribe(res => {
if (res instanceof RestOccurError) {
const { payload: err = {} as HttpErrorResponse | any } = res;
const body = snq(
() => (err as HttpErrorResponse).error.error,
DEFAULT_ERROR_MESSAGES.defaultError.title,
);
if (err instanceof HttpErrorResponse && err.headers.get('_AbpErrorFormat')) {
const confirmation$ = this.showError(null, null, body);
if (err.status === 401) {
confirmation$.subscribe(() => {
this.navigateToLogin();
});
break;
case 0:
if ((err as HttpErrorResponse).statusText === 'Unknown Error') {
}
} else {
switch ((err as HttpErrorResponse).status) {
case 401:
this.canCreateCustomError(401)
? this.show401Page()
: this.showError(
{
key: 'AbpAccount::DefaultErrorMessage401',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.title,
},
{
key: 'AbpAccount::DefaultErrorMessage401Detail',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.details,
},
).subscribe(() => this.navigateToLogin());
break;
case 403:
this.createErrorComponent({
title: {
key: 'AbpAccount::DefaultErrorMessage',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title,
key: 'AbpAccount::DefaultErrorMessage403',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.title,
},
details: {
key: 'AbpAccount::DefaultErrorMessage403Detail',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.details,
},
status: 403,
});
}
break;
default:
this.showError(DEFAULT_ERROR_MESSAGES.defaultError.details, DEFAULT_ERROR_MESSAGES.defaultError.title);
break;
break;
case 404:
this.canCreateCustomError(404)
? this.show404Page()
: this.showError(
{
key: 'AbpAccount::DefaultErrorMessage404',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.details,
},
{
key: 'AbpAccount::DefaultErrorMessage404Detail',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.title,
},
);
break;
case 500:
this.createErrorComponent({
title: {
key: 'AbpAccount::500Message',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.title,
},
details: {
key: 'AbpAccount::InternalServerErrorMessage',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.details,
},
status: 500,
});
break;
case 0:
if ((err as HttpErrorResponse).statusText === 'Unknown Error') {
this.createErrorComponent({
title: {
key: 'AbpAccount::DefaultErrorMessage',
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title,
},
});
}
break;
default:
this.showError(
DEFAULT_ERROR_MESSAGES.defaultError.details,
DEFAULT_ERROR_MESSAGES.defaultError.title,
);
break;
}
}
} else if (
res instanceof RouterError &&
snq(() => res.event.error.indexOf('Cannot match') > -1, false)
) {
this.show404Page();
} else if (res instanceof RouterDataResolved && this.componentRef) {
this.componentRef.destroy();
this.componentRef = null;
}
} else if (res instanceof RouterError && snq(() => res.event.error.indexOf('Cannot match') > -1, false)) {
this.show404Page();
} else if (res instanceof RouterDataResolved && this.componentRef) {
this.componentRef.destroy();
this.componentRef = null;
}
});
});
}
private show401Page() {
@ -179,6 +190,9 @@ export class ErrorHandler {
if (body.details) {
message = body.details;
title = body.message;
} else if (body.code) {
title = body.code;
message = body.message;
} else {
message = body.message || DEFAULT_ERROR_MESSAGES.defaultError.title;
}
@ -192,7 +206,9 @@ export class ErrorHandler {
private navigateToLogin() {
this.store.dispatch(
new Navigate(['/account/login'], null, { state: { redirectUrl: this.store.selectSnapshot(RouterState.url) } }),
new Navigate(['/account/login'], null, {
state: { redirectUrl: this.store.selectSnapshot(RouterState.url) },
}),
);
}
@ -200,7 +216,9 @@ export class ErrorHandler {
const renderer = this.rendererFactory.createRenderer(null, null);
const host = renderer.selectRootElement(document.body, true);
this.componentRef = this.cfRes.resolveComponentFactory(HttpErrorWrapperComponent).create(this.injector);
this.componentRef = this.cfRes
.resolveComponentFactory(HttpErrorWrapperComponent)
.create(this.injector);
for (const key in this.componentRef.instance) {
if (this.componentRef.instance.hasOwnProperty(key)) {

Loading…
Cancel
Save