forked from vercel/micro
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patherrors.ts
33 lines (27 loc) · 831 Bytes
/
errors.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
// Packages
import bytes from 'bytes';
import { RawBodyError } from './types';
export class MicriError extends Error {
statusCode: number;
code: string;
originalError: Error | null;
constructor(statusCode: number, code: string, message: string, originalError?: Error) {
super(message);
this.statusCode = statusCode;
this.code = code;
this.originalError = originalError || null;
}
}
export class MicriBodyError extends MicriError {
constructor(err: RawBodyError, limit: string | number) {
let statusCode = 400;
let code = 'invalid_body';
let message = 'Invalid body';
if (err.type === 'entity.too.large') {
statusCode = 413;
code = 'request_entity_too_large';
message = `Body exceeded ${typeof limit === 'string' ? limit : bytes(limit)} limit`;
}
super(statusCode, code, message, err);
}
}