Skip to content

Commit ff5e66e

Browse files
committed
feat: add round
1 parent 7d12b72 commit ff5e66e

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/index.test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,24 @@ describe('Error0', () => {
267267
expect(AppError.serialize(recreated, false)).toEqual(json)
268268
})
269269

270+
it('.round() static and instance do serialize/from roundtrip', () => {
271+
const AppError = Error0.use(statusPlugin).use(codePlugin)
272+
const error = new AppError('test', { status: 409, code: 'NOT_FOUND' })
273+
const roundedStatic = AppError.round(error, false)
274+
const roundedInstance = error.round(false)
275+
276+
expect(roundedStatic).toBeInstanceOf(AppError)
277+
expect(roundedInstance).toBeInstanceOf(AppError)
278+
expect(roundedStatic.status).toBe(409)
279+
expect(roundedStatic.code).toBe('NOT_FOUND')
280+
expect(roundedInstance.status).toBe(409)
281+
expect(roundedInstance.code).toBe('NOT_FOUND')
282+
expectTypeOf(roundedStatic.status).toEqualTypeOf<number | undefined>()
283+
expectTypeOf(roundedStatic.code).toEqualTypeOf<'NOT_FOUND' | 'BAD_REQUEST' | 'UNAUTHORIZED' | undefined>()
284+
expectTypeOf(roundedInstance.status).toEqualTypeOf<number | undefined>()
285+
expectTypeOf(roundedInstance.code).toEqualTypeOf<'NOT_FOUND' | 'BAD_REQUEST' | 'UNAUTHORIZED' | undefined>()
286+
})
287+
270288
it('.serialize() floated props and not serialize causes', () => {
271289
const AppError = Error0.use(statusPlugin).use(codePlugin)
272290
const error1 = new AppError('test', { status: 409 })
@@ -277,7 +295,6 @@ describe('Error0', () => {
277295
expect('cause' in json).toBe(false)
278296
})
279297

280-
281298
it('by default causes not serialized', () => {
282299
const AppError = Error0.use(statusPlugin).use(codePlugin)
283300
const error = new AppError('test', { status: 400, code: 'NOT_FOUND' })
@@ -554,7 +571,6 @@ describe('Error0', () => {
554571
expect(error1.code).toBe(undefined)
555572
})
556573

557-
558574
it('messages can be combined on serialization', () => {
559575
const AppError = Error0.use(statusPlugin)
560576
.use(codePlugin)
@@ -609,7 +625,6 @@ describe('Error0', () => {
609625
`)
610626
})
611627

612-
613628
it('Error0 assignable to LikeError0', () => {
614629
type LikeError0 = {
615630
from: (error: unknown) => Error

src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,10 @@ export type ClassError0<TPluginsMap extends ErrorPluginsMap = EmptyPluginsMap> =
404404
from: (
405405
error: unknown,
406406
) => Error0 & ErrorResolved<TPluginsMap> & ErrorOwnMethods<TPluginsMap> & ErrorResolveMethods<TPluginsMap>
407+
round: (
408+
error: unknown,
409+
isPublic?: boolean,
410+
) => Error0 & ErrorResolved<TPluginsMap> & ErrorOwnMethods<TPluginsMap> & ErrorResolveMethods<TPluginsMap>
407411
resolve: (error: unknown) => ErrorResolvedProps<TPluginsMap>
408412
serialize: (error: unknown, isPublic?: boolean) => Record<string, unknown>
409413
own: {
@@ -713,6 +717,10 @@ export class Error0 extends Error {
713717
return this._fromNonError0(error)
714718
}
715719

720+
static round(error: unknown, isPublic = false): Error0 {
721+
return this.from(this.serialize(error, isPublic))
722+
}
723+
716724
private static _applyAdapt(error: Error0): Error0 {
717725
const plugin = this._getResolvedPlugin()
718726
for (const adapt of plugin.adapt) {
@@ -1075,4 +1083,9 @@ export class Error0 extends Error {
10751083
const ctor = this.constructor as typeof Error0
10761084
return ctor.serialize(this, isPublic)
10771085
}
1086+
1087+
round<TThis extends Error0>(this: TThis, isPublic = true): TThis {
1088+
const ctor = this.constructor as typeof Error0
1089+
return ctor.round(this, isPublic) as TThis
1090+
}
10781091
}

0 commit comments

Comments
 (0)