-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemDetail.ts
More file actions
51 lines (48 loc) · 1.25 KB
/
ProblemDetail.ts
File metadata and controls
51 lines (48 loc) · 1.25 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
import {
Type,
type Static,
type TEnum,
type TLiteral,
type TObject,
type TOptional,
type TString,
} from '@sinclair/typebox'
import { HttpStatusCode, StatusCode } from './HttpStatusCode.ts'
export const Context = 'https://datatracker.ietf.org/doc/rfc9457/'
/**
* Problem Details Object
*
* @see https://datatracker.ietf.org/doc/rfc9457/
*/
export const ProblemDetail: TObject<{
'@context': TLiteral<typeof Context>
'@id': TOptional<TString>
type: TOptional<TString>
status: TOptional<TEnum<typeof HttpStatusCode>>
title: TString
detail: TOptional<TString>
}> = Type.Object(
{
'@context': Type.Literal(Context),
'@id': Type.Optional(Type.String()),
type: Type.Optional(Type.String()),
status: Type.Optional(StatusCode),
title: Type.String(),
detail: Type.Optional(Type.String()),
},
{
title: 'Problem Detail',
description: 'See see https://datatracker.ietf.org/doc/rfc9457/',
},
)
/**
* Can be used to throw an error with a Problem Detail object.
*/
export class ProblemDetailError extends Error {
public readonly problem: Static<typeof ProblemDetail>
constructor(problem: Omit<Static<typeof ProblemDetail>, '@context'>) {
super(problem.title)
this.problem = { '@context': Context, ...problem }
this.name = 'ProblemDetailError'
}
}