Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typeguard for ResponseStatusError #97

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {
type InternalErrorParams,
} from './src/errors/InternalError'
export { ResponseStatusError } from './src/errors/ResponseStatusError'
export { isResponseStatusError } from './src/errors/errorTypeGuards'

export { ConfigScope } from './src/config/ConfigScope'
export { ensureClosingSlashTransformer } from './src/config/configTransformers'
Expand Down
1 change: 1 addition & 0 deletions src/errors/ResponseStatusError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { InternalError } from './InternalError'
export class ResponseStatusError extends InternalError {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public readonly response: RequestResult<any>
public readonly isResponseStatusError = true

// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(requestResult: RequestResult<any>, requestLabel = 'N/A') {
Expand Down
25 changes: 25 additions & 0 deletions src/errors/errorTypeGuards.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe } from 'vitest'

import { InternalError } from './InternalError'
import { ResponseStatusError } from './ResponseStatusError'
import { isResponseStatusError } from './errorTypeGuards'

describe('errorTypeGuards', () => {
describe('isResponseStatusError', () => {
it('Returns true for ResponseStatusError', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-explicit-any
const error = new ResponseStatusError({} as any, 'label')

expect(isResponseStatusError(error)).toBe(true)
})

it('Returns false for not a ResponseStatusError', () => {
const error = new InternalError({
message: 'message',
errorCode: 'CODE',
})

expect(isResponseStatusError(error)).toBe(false)
})
})
})
5 changes: 5 additions & 0 deletions src/errors/errorTypeGuards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { ResponseStatusError } from './ResponseStatusError'

export function isResponseStatusError(entity: unknown): entity is ResponseStatusError {
return 'isResponseStatusError' in (entity as ResponseStatusError)
}