Repository navigation
Expand file tree
/
Copy patherrors.ts
More file actions
170 lines (155 loc) · 4.64 KB
/
Copy patherrors.ts
File metadata and controls
170 lines (155 loc) · 4.64 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* @peac/api/errors - RFC 9457 Problem Details error generator
* Standardized error responses for PEAC verify API
*/
import type { ProblemDetails, VerifyErrorDetails, ErrorContext, HttpStatus } from './types.js';
export class ProblemError extends Error {
constructor(
public status: HttpStatus,
public problemType: string,
public title: string,
public detail?: string,
public instance?: string,
public extensions?: Record<string, unknown>
) {
super(title);
this.name = 'ProblemError';
}
toProblemDetails(): ProblemDetails {
return {
type: this.problemType,
title: this.title,
status: this.status,
detail: this.detail,
instance: this.instance,
...this.extensions,
};
}
}
/** Shape used by isProblemError for duck-typed detection */
interface ProblemErrorLike {
name: string;
status: number;
toProblemDetails: () => ProblemDetails;
}
/**
* Duck-typed ProblemError detection.
*
* Bundlers (tsup) can duplicate the ProblemError class, breaking instanceof.
* This predicate checks by shape so error handlers work across bundle boundaries.
*/
export function isProblemError(err: unknown): err is ProblemErrorLike {
return (
err instanceof Error &&
err.name === 'ProblemError' &&
typeof (err as any).toProblemDetails === 'function'
);
}
export function createProblemDetails(ctx: ErrorContext, instance?: string): VerifyErrorDetails {
const baseUrl = 'https://www.peacprotocol.org/problems';
const problems: Record<string, { status: number; title: string; detail: string }> = {
'invalid-jws-format': {
status: 400,
title: 'Invalid JWS Format',
detail: 'The provided receipt is not a valid JWS compact serialization',
},
'missing-receipt': {
status: 400,
title: 'Missing Receipt',
detail: 'Receipt parameter is required in request body',
},
'invalid-signature': {
status: 422,
title: 'Invalid Signature',
detail: 'Receipt signature verification failed',
},
'unknown-key-id': {
status: 422,
title: 'Unknown Key ID',
detail: 'The kid in the receipt header is not recognized',
},
'schema-validation-failed': {
status: 422,
title: 'Schema Validation Failed',
detail: 'Receipt payload does not conform to PEAC schema',
},
'expired-receipt': {
status: 422,
title: 'Expired Receipt',
detail: 'Receipt timestamp is outside acceptable window',
},
'processing-error': {
status: 500,
title: 'Processing Error',
detail: 'An internal error occurred while processing the receipt',
},
};
const problem = problems[ctx.code] || problems['processing-error'];
return {
type: `${baseUrl}/${ctx.code}`,
title: problem.title,
status: problem.status,
detail: ctx.details?.join('; ') || problem.detail,
instance,
'peac-error-code': ctx.code,
'peac-trace-id': ctx.traceId,
'validation-failures': ctx.details,
};
}
export function handleVerifyError(
error: unknown,
instance?: string
): { status: HttpStatus; body: VerifyErrorDetails } {
let ctx: ErrorContext;
if (error instanceof ProblemError) {
return {
status: error.status,
body: error.toProblemDetails() as VerifyErrorDetails,
};
}
if (error instanceof Error) {
// Map common errors to structured responses
if (error.message.includes('Invalid JWS')) {
ctx = { code: 'invalid-jws-format', category: 'validation' };
} else if (error.message.includes('Unknown key')) {
ctx = { code: 'unknown-key-id', category: 'authentication' };
} else if (error.message.includes('signature')) {
ctx = { code: 'invalid-signature', category: 'validation' };
} else if (error.message.includes('schema')) {
ctx = { code: 'schema-validation-failed', category: 'validation' };
} else {
ctx = {
code: 'processing-error',
category: 'processing',
details: [error.message],
};
}
} else {
ctx = {
code: 'processing-error',
category: 'processing',
details: [String(error)],
};
}
const problemDetails = createProblemDetails(ctx, instance);
return {
status: problemDetails.status as HttpStatus,
body: problemDetails,
};
}
// Convenience function for validation errors
export function validationError(
details: string[],
instance?: string
): { status: HttpStatus; body: VerifyErrorDetails } {
const ctx: ErrorContext = {
code: 'schema-validation-failed',
category: 'validation',
details,
};
const problemDetails = createProblemDetails(ctx, instance);
return {
status: 422,
body: problemDetails,
};
}