forked from cloudflare/privacypass-issuer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.ts
112 lines (93 loc) · 2.55 KB
/
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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2023 Cloudflare, Inc.
// SPDX-License-Identifier: Apache-2.0
import { Labels } from 'promjs';
import { Context } from './context';
import { JSONResponse } from './utils/jsonResponse';
function shouldSendToSentry(error: Error): boolean {
if (
error instanceof PageNotFoundError ||
error instanceof MethodNotAllowedError ||
error instanceof HeaderNotDefinedError
) {
return false;
}
return true;
}
export async function handleError(ctx: Context, error: Error, labels?: Labels) {
console.error(error.stack);
ctx.metrics.erroredRequestsTotal.inc(labels);
const status = (error as HTTPError).status ?? 500;
const message = error.message || 'Server Error';
console.log(message);
if (shouldSendToSentry(error)) {
ctx.logger.captureException(error);
}
return new JSONResponse(
{
error: { reason: error.name, details: message },
},
{ status }
);
}
export class HTTPError extends Error {
status: number;
constructor(message?: string, status = 500) {
super(message);
this.name = 'HTTPError';
this.status = status;
}
}
export class MethodNotAllowedError extends HTTPError {
static CODE = 'ERROR_METHOD_NOT_ALLOWED';
code: string;
constructor(message = 'Method not allowed') {
super(message, 405);
this.name = 'MethodNotAllowed';
this.code = MethodNotAllowedError.CODE;
}
}
export class PageNotFoundError extends HTTPError {
static CODE = 'ERROR_PAGE_NOT_FOUND';
code: string;
constructor(message = 'Page not found') {
super(message, 404);
this.name = 'PageNotFound';
this.code = PageNotFoundError.CODE;
}
}
export class HeaderNotDefinedError extends HTTPError {
static CODE = 'ERROR_HEADER_NOT_DEFINED';
code: string;
constructor(message = 'Header not defined') {
super(message, 406);
this.name = 'HeaderNotDefined';
this.code = HeaderNotDefinedError.CODE;
}
}
export class InternalCacheError extends HTTPError {
static CODE = 'ERROR_INTERNAL_CACHE_ERROR';
code: string;
constructor(message = 'Internal cache error') {
super(message, 500);
this.name = 'InternalCacheError';
this.code = InternalCacheError.CODE;
}
}
export class NotImplementedError extends HTTPError {
static CODE = 'ERROR_NOT_IMPLEMENTED';
code: string;
constructor(message = 'Not Implemented') {
super(message, 501);
this.name = 'NotImplemented';
this.code = NotImplementedError.CODE;
}
}
export class UnreachableError extends HTTPError {
static CODE = 'ERROR_UNREACHABLE';
code: string;
constructor(message = 'Unreachable') {
super(message, 500);
this.name = 'Unreachable';
this.code = UnreachableError.CODE;
}
}