Skip to content

Commit

Permalink
test: add unit tests for "InternalError"
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed May 8, 2024
1 parent 826fb09 commit 6ca4cad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/core/utils/internal/devUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { InternalError } from './devUtils'

describe(InternalError, () => {
it('creates an InternalError instance', () => {
const error = new InternalError('Message')

expect(error.name).toBe('InternalError')
expect(error.message).toBe('Message')
expect(error.toString()).toBe('InternalError: Message')
expect(error.stack).toMatch(/\w+/)
})

it('passes the identity check', () => {
const error = new InternalError('Message')
expect(error instanceof InternalError).toBe(true)
expect(error instanceof Error).toBe(true)

const extraneousError = new Error('Message')
expect(extraneousError).not.toBeInstanceOf(InternalError)
})
})
11 changes: 10 additions & 1 deletion src/core/utils/internal/devUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export const devUtils = {
error,
}

/**
* Internal error instance.
* Used to differentiate the library errors that must be forwarded
* to the user from the unhandled exceptions. Use this if you don't
* wish for the error to be coerced to a 500 fallback response.
*/
export class InternalError extends Error {
//
constructor(message: string) {
super(message)
this.name = 'InternalError'
}
}

0 comments on commit 6ca4cad

Please sign in to comment.