Skip to content

Commit

Permalink
fix(api-error): fix ApiErrorHandler cannot detected
Browse files Browse the repository at this point in the history
- fix ApiErrorHandler isSelected function
- change ApiError constructor: $option >  option
  • Loading branch information
imjuni committed Feb 23, 2024
1 parent 0b2f0aa commit 8671ec2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/errors/ApiError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ApiError<TDATA_TYPE = unknown> extends Error {
| Error
| ApiError<TDATA_TYPE>
| (TPartialApiErrorReplyArgs<TDATA_TYPE> & {
$option?: Partial<IApiErrorOption>;
option?: Partial<IApiErrorOption>;
}),
) {
if (typeof args === 'string') {
Expand Down Expand Up @@ -82,7 +82,7 @@ export class ApiError<TDATA_TYPE = unknown> extends Error {
this.#option = option;
} else {
const reply = ApiError.getRestErrorReply(args);
const option = ApiError.getRestErrorOption(args?.$option);
const option = ApiError.getRestErrorOption(args?.option);

super(reply.message);

Expand Down
2 changes: 1 addition & 1 deletion src/errors/__tests__/rest.error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('RestError', () => {
header: { 'accept-language': 'en' },
logging: { info: 'message' },
};
const err = new ApiError({ ...reply, $option: option });
const err = new ApiError({ ...reply, option });
expect(err.reply).toMatchObject(reply);
expect(err.option).toMatchObject(option);
});
Expand Down
6 changes: 5 additions & 1 deletion src/handlers/ApiErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ApiErrorHandler extends HTTPErrorHandler {
return false;
}

if (args.err instanceof ApiError) {
if (!(args.err instanceof ApiError)) {
return false;
}

Expand Down Expand Up @@ -58,6 +58,10 @@ export class ApiErrorHandler extends HTTPErrorHandler {
? EncryptContiner.it.encrypt(code)
: code;

if (typeof payload === 'object') {
return { code: encrypted, ...payload, message };
}

return { code: encrypted, payload, message };
}

Expand Down
4 changes: 3 additions & 1 deletion src/handlers/SchemaErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ApiError } from '#/errors/ApiError';
import { HTTPErrorHandler } from '#/handlers/HTTPErrorHandler';
import type { THTTPErrorHandlerParameters } from '#/handlers/interfaces/THTTPErrorHandlerParameters';
import { getSourceLocation } from '#/modules/getSourceLocation';
Expand Down Expand Up @@ -26,7 +27,8 @@ export class SchemaErrorHandler extends HTTPErrorHandler {
protected preHook(args: THTTPErrorHandlerParameters): void {
super.preHook(args);

args.reply.status(args.err.statusCode ?? httpStatusCodes.BAD_REQUEST);
const statusCode = args.err instanceof ApiError ? args.err.reply.status : args.err.statusCode;
args.reply.status(statusCode ?? httpStatusCodes.BAD_REQUEST);
}

protected postHook = noop;
Expand Down

0 comments on commit 8671ec2

Please sign in to comment.