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

Adding EntityGoneError #133

Merged
merged 5 commits into from
May 16, 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
9 changes: 5 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export {
type InternalErrorParams,
} from './src/errors/InternalError'
export { ResponseStatusError } from './src/errors/ResponseStatusError'
export { isResponseStatusError } from './src/errors/errorTypeGuards'
export { isResponseStatusError, isEntityGoneError } from './src/errors/errorTypeGuards'

export { ConfigScope } from './src/config/ConfigScope'
export { ensureClosingSlashTransformer } from './src/config/configTransformers'
Expand Down Expand Up @@ -94,12 +94,13 @@ export {
export { type MayOmit } from './src/common/may-omit'
export { type FreeformRecord } from './src/common/commonTypes'
export {
RequestValidationError,
AccessDeniedError,
EntityNotFoundError,
type ValidationError,
type CommonErrorParams,
type OptionalMessageErrorParams,
RequestValidationError,
AccessDeniedError,
EntityNotFoundError,
EntityGoneError,
AuthFailedError,
} from './src/errors/publicErrors'

Expand Down
28 changes: 27 additions & 1 deletion src/errors/errorTypeGuards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { describe } from 'vitest'

import { InternalError } from './InternalError'
import { ResponseStatusError } from './ResponseStatusError'
import { isResponseStatusError } from './errorTypeGuards'
import { isEntityGoneError, isResponseStatusError } from './errorTypeGuards'
import { EntityGoneError, EntityNotFoundError } from './publicErrors'

describe('errorTypeGuards', () => {
describe('isResponseStatusError', () => {
Expand All @@ -22,4 +23,29 @@ describe('errorTypeGuards', () => {
expect(isResponseStatusError(error)).toBe(false)
})
})

describe('isEntityGoneError', () => {
it('Returns true for EntityGoneError', () => {
const error = new EntityGoneError({
message: 'message',
})

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

it('Returns false for not a EntityGoneError', () => {
const errors = [
'whatever string',
1,
new Error('message'),
new InternalError({
message: 'message',
errorCode: 'CODE',
}),
new EntityNotFoundError({ message: 'message' }),
]

expect(errors.map(isEntityGoneError).every((e) => e === false)).toBe(true)
})
})
})
7 changes: 7 additions & 0 deletions src/errors/errorTypeGuards.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { isPublicNonRecoverableError } from '../utils/typeUtils'

import type { ResponseStatusError } from './ResponseStatusError'
import type { EntityGoneError } from './publicErrors'

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

export function isEntityGoneError(entity: unknown): entity is EntityGoneError {
return isPublicNonRecoverableError(entity) && entity.httpStatusCode === 410
}
11 changes: 11 additions & 0 deletions src/errors/publicErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ export class EntityNotFoundError extends PublicNonRecoverableError {
}
}

export class EntityGoneError extends PublicNonRecoverableError {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we ever need to instanceof check it? wonder if we need a flag on it, and a typeguard

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, yeah, we would need the instanceof in some cases

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeguard added, not sure what do you mean by the flag, but please have a look to see if it makes sense 🙏

Note: all 410 http errors considered as entityGone, no matter if it was created as a PublincNonRecoverableError

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

common technique for simple typeguards is adding a field like "isEntityGone: true" so that you could do a single-field check, but you are right, in this case http code check is a better option!

constructor(params: CommonErrorParams) {
super({
message: params.message,
errorCode: 'ENTITY_GONE',
httpStatusCode: 410,
details: params.details,
})
}
}

export class AuthFailedError extends PublicNonRecoverableError {
constructor(params: OptionalMessageErrorParams = {}) {
super({
Expand Down
Loading