Skip to content

Commit

Permalink
Merge 382b188 into 85dc0ba
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Jun 19, 2024
2 parents 85dc0ba + 382b188 commit d441a0f
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions packages/api/src/utils/client/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ export class ApiResponse<E extends Endpoint> 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<string> {
if (!this._errorBody) {
if (this._errorBody === undefined) {
this._errorBody = await this.text();
}
return this._errorBody;
Expand All @@ -179,23 +179,24 @@ export class ApiResponse<E extends Endpoint> 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;
}
}

0 comments on commit d441a0f

Please sign in to comment.