Skip to content

Commit 0e07aaf

Browse files
committed
feat: add extra to HttpError instances
1 parent a8c8e0b commit 0e07aaf

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

.changeset/cold-poets-serve.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@otterhttp/errors": minor
3+
---
4+
5+
feat: add `extra` to HttpError instances, intended for adding additional fields to JSON response payloads

packages/errors/src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@ import ModuleError from "module-error"
33

44
import { HttpStatus, type StatusCode, isValidStatusCode, statusMessages } from "./status-codes"
55

6+
type ExtraLiteral = string | number | boolean | null | undefined | { [property: string]: ExtraLiteral } | ExtraLiteral[]
7+
68
type ModuleErrorOptions = ConstructorParameters<typeof ModuleError>[1]
79
export type HttpErrorOptions = ModuleErrorOptions & {
810
statusCode?: StatusCode
911
statusMessage?: string
1012
exposeMessage?: boolean
1113
headers?: OutgoingHttpHeaders
14+
extra?: Record<string, ExtraLiteral>
1215
}
1316

1417
export abstract class HttpError extends ModuleError {
1518
statusCode: StatusCode
1619
statusMessage: string
1720
exposeMessage: boolean
1821
headers: OutgoingHttpHeaders
22+
extra: Record<string, ExtraLiteral>
1923

2024
static {
2125
HttpError.prototype.name = "HttpError"
@@ -36,12 +40,14 @@ export abstract class HttpError extends ModuleError {
3640
if (options.statusMessage != null) this.statusMessage = options.statusMessage
3741
if (options.exposeMessage != null) this.exposeMessage = options.exposeMessage
3842
if (options.headers != null) this.headers = options.headers
43+
if (options.extra != null) this.extra = options.extra
3944
}
4045

4146
this.statusCode ??= HttpStatus.InternalServerError
4247
this.statusMessage ??= statusMessages[this.statusCode]
4348
this.exposeMessage ??= this.statusCode < 500
4449
this.headers ??= {}
50+
this.extra ??= {}
4551
}
4652
}
4753

0 commit comments

Comments
 (0)