-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
reject.action.ts
63 lines (57 loc) · 1.79 KB
/
reject.action.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {inject, Next} from '@loopback/context';
import {HttpError} from 'http-errors';
import {ErrorWriterOptions, writeErrorToResponse} from 'strong-error-handler';
import {RestBindings} from '../keys';
import {
HandlerContext,
HttpContext,
LogError,
Reject,
restAction,
} from '../types';
import {BaseRestAction} from './base-action';
// TODO(bajtos) Make this mapping configurable at RestServer level,
// allow apps and extensions to contribute additional mappings.
const codeToStatusCodeMap: {[key: string]: number} = {
ENTITY_NOT_FOUND: 404,
};
@restAction('reject')
export class RejectAction extends BaseRestAction {
constructor(
@inject(RestBindings.SequenceActions.LOG_ERROR)
protected logError: LogError,
@inject(RestBindings.ERROR_WRITER_OPTIONS, {optional: true})
protected errorWriterOptions?: ErrorWriterOptions,
) {
super();
}
async intercept(ctx: HttpContext, next: Next) {
try {
return await next();
} catch (error) {
await this.delegate(ctx, 'reject', [error]);
}
}
reject(
@inject(RestBindings.Http.CONTEXT) {request, response}: HandlerContext,
error: Error,
) {
const err = error as HttpError;
if (!err.status && !err.statusCode && err.code) {
const customStatus = codeToStatusCodeMap[err.code];
if (customStatus) {
err.statusCode = customStatus;
}
}
const statusCode = err.statusCode || err.status || 500;
writeErrorToResponse(err, request, response, this.errorWriterOptions);
this.logError(err, statusCode, request);
}
get handler(): Reject {
return this.reject.bind(this);
}
}