|
1 | 1 | import { Context } from 'cordis';
|
2 | 2 | import proxy from 'koa-proxies';
|
3 |
| -import { ForbiddenError, WebService } from '@hydrooj/framework'; |
| 3 | +import { ForbiddenError, HydroError, NotFoundError, UserFacingError, WebService } from '@hydrooj/framework'; |
4 | 4 | import { config } from '../config';
|
5 | 5 | import { randomstring } from '../utils';
|
| 6 | +import { errorMessage } from '@hydrooj/utils'; |
6 | 7 | export * from '@hydrooj/framework/decorators';
|
7 | 8 |
|
8 | 9 | export async function apply(pluginContext: Context) {
|
@@ -56,5 +57,35 @@ export async function apply(pluginContext: Context) {
|
56 | 57 | },
|
57 | 58 | })(ctx, next);
|
58 | 59 | });
|
| 60 | + server.httpHandlerMixin({ |
| 61 | + async onerror(error: HydroError) { |
| 62 | + error.msg ||= () => error.message; |
| 63 | + if (error instanceof UserFacingError && !process.env.DEV) error.stack = ''; |
| 64 | + if (!(error instanceof NotFoundError) && !('nolog' in error)) { |
| 65 | + // eslint-disable-next-line max-len |
| 66 | + console.error(`${this.request.method}: ${this.request.path}`, error.msg(), error.params); |
| 67 | + if (error.stack) console.error(error.stack); |
| 68 | + } |
| 69 | + this.response.status = error instanceof UserFacingError ? error.code : 500; |
| 70 | + this.response.body = { |
| 71 | + UserFacingError, |
| 72 | + error: { message: error.msg(), params: error.params, stack: errorMessage(error.stack || '') }, |
| 73 | + }; |
| 74 | + }, |
| 75 | + }); |
| 76 | + server.wsHandlerMixin({ |
| 77 | + async onerror(err: HydroError) { |
| 78 | + console.error(`Path:${this.request.path}`); |
| 79 | + console.error(err); |
| 80 | + if (err instanceof UserFacingError) err.stack = this.request.path; |
| 81 | + this.send({ |
| 82 | + error: { |
| 83 | + name: err.name, |
| 84 | + params: err.params || [], |
| 85 | + }, |
| 86 | + }); |
| 87 | + this.close(4000, err.toString()); |
| 88 | + }, |
| 89 | + }); |
59 | 90 | });
|
60 | 91 | }
|
0 commit comments