From 382b188e7179ccaf0f8600bc02214436c95fe21b Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Wed, 19 Jun 2024 17:08:10 +0100 Subject: [PATCH] fix: default to http status text if error response is empty --- packages/api/src/utils/client/response.ts | 27 ++++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/api/src/utils/client/response.ts b/packages/api/src/utils/client/response.ts index b7039f4865ae..ed008273588a 100644 --- a/packages/api/src/utils/client/response.ts +++ b/packages/api/src/utils/client/response.ts @@ -161,11 +161,11 @@ export class ApiResponse extends Response { return null; } - return new ApiError(getErrorMessage(this.resolvedErrorBody()), this.status, this.definition.operationId); + return new ApiError(this.getErrorMessage(), this.status, this.definition.operationId); } async errorBody(): Promise { - if (!this._errorBody) { + if (this._errorBody === undefined) { this._errorBody = await this.text(); } return this._errorBody; @@ -179,23 +179,24 @@ export class ApiResponse extends Response { } private resolvedErrorBody(): string { - if (!this._errorBody) { + if (this._errorBody === undefined) { throw Error("errorBody() must be called first"); } return this._errorBody; } -} -function getErrorMessage(errBody: string): string { - try { - const errJson = JSON.parse(errBody) as {message?: string}; - if (errJson.message) { - return errJson.message; - } else { - return errBody; + private getErrorMessage(): string { + const errBody = this.resolvedErrorBody(); + try { + const errJson = JSON.parse(errBody) as {message?: string}; + if (errJson.message) { + return errJson.message; + } else { + return errBody; + } + } catch (e) { + return errBody || this.statusText; } - } catch (e) { - return errBody; } }