diff --git a/src/lib/seam/connect/api-error-types.ts b/src/lib/seam/connect/api-error-types.ts index a2bddbe..e9ca5a9 100644 --- a/src/lib/seam/connect/api-error-types.ts +++ b/src/lib/seam/connect/api-error-types.ts @@ -8,4 +8,5 @@ export interface ApiError { type: string message: string data?: unknown + validation_errors?: Record } diff --git a/src/lib/seam/connect/seam-http-error.ts b/src/lib/seam/connect/seam-http-error.ts index 91129e9..7e8dfe0 100644 --- a/src/lib/seam/connect/seam-http-error.ts +++ b/src/lib/seam/connect/seam-http-error.ts @@ -46,11 +46,17 @@ export const isSeamHttpUnauthorizedError = ( export class SeamHttpInvalidInputError extends SeamHttpApiError { override code: 'invalid_input' + readonly #validationErrors: NonNullable 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 ?? [] } } diff --git a/test/seam/connect/http-error.test.ts b/test/seam/connect/http-error.test.ts index 05920bc..2380f3f 100644 --- a/test/seam/connect/http-error.test.ts +++ b/test/seam/connect/http-error.test.ts @@ -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', + ]) })