From a5c70d3e58755250f36e67207dea76275bf86807 Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Fri, 12 Jul 2024 17:50:20 +0000 Subject: [PATCH] feat: Add SeamHttpInvalidInputError.getValidationErrorMessages (#132) * feat: Add SeamHttpInvalidInputError.getInputErrorMessages * ci: Format code * Rename to getValidationErrorMessages --------- Co-authored-by: Seam Bot --- src/lib/seam/connect/api-error-types.ts | 1 + src/lib/seam/connect/seam-http-error.ts | 6 ++++++ test/seam/connect/http-error.test.ts | 3 +++ 3 files changed, 10 insertions(+) diff --git a/src/lib/seam/connect/api-error-types.ts b/src/lib/seam/connect/api-error-types.ts index a2bddbeb..e9ca5a9a 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 91129e9f..7e8dfe04 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 05920bcd..2380f3f5 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', + ]) })