Skip to content

Commit

Permalink
feat: Add SeamHttpInvalidInputError.getValidationErrorMessages (#132)
Browse files Browse the repository at this point in the history
* feat: Add SeamHttpInvalidInputError.getInputErrorMessages

* ci: Format code

* Rename to getValidationErrorMessages

---------

Co-authored-by: Seam Bot <[email protected]>
  • Loading branch information
razor-x and seambot authored Jul 12, 2024
1 parent 295dc17 commit a5c70d3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib/seam/connect/api-error-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface ApiError {
type: string
message: string
data?: unknown
validation_errors?: Record<string, { _errors: string[] }>
}
6 changes: 6 additions & 0 deletions src/lib/seam/connect/seam-http-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ export const isSeamHttpUnauthorizedError = (

export class SeamHttpInvalidInputError extends SeamHttpApiError {
override code: 'invalid_input'
readonly #validationErrors: NonNullable<ApiError['validation_errors']>

constructor(error: ApiError, statusCode: number, requestId: string) {
super(error, statusCode, requestId)
this.name = this.constructor.name
this.code = 'invalid_input'
this.#validationErrors = error.validation_errors ?? {}
}

getValidationErrorMessages(paramName: string): string[] {
return this.#validationErrors[paramName]?._errors ?? []
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/seam/connect/http-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,7 @@ test('SeamHttp: throws SeamHttpInvalidInputError on invalid input', async (t) =>
t.is(err?.statusCode, 400)
t.is(err?.code, 'invalid_input')
t.true(err?.requestId?.startsWith('request'))
t.deepEqual(err?.getValidationErrorMessages('device_ids'), [
'Expected array, received number',
])
})

0 comments on commit a5c70d3

Please sign in to comment.