diff options
author | Florian Dold <florian@dold.me> | 2023-11-21 11:51:14 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2023-11-21 11:51:14 +0100 |
commit | 342df60abe6d8b8b0c51e79cf13c7edcca58d9f9 (patch) | |
tree | fb75d228c98583b812cbe483ad725ca2f1ce5451 /packages/taler-util | |
parent | 39897b0c24eec28e86235351b451ca8f9e26e8c7 (diff) | |
download | wallet-core-342df60abe6d8b8b0c51e79cf13c7edcca58d9f9.tar.xz |
improve error message when error response doesn't even contain JSON
Diffstat (limited to 'packages/taler-util')
-rw-r--r-- | packages/taler-util/src/http-common.ts | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/packages/taler-util/src/http-common.ts b/packages/taler-util/src/http-common.ts index da2fbb9da..e934c07f7 100644 --- a/packages/taler-util/src/http-common.ts +++ b/packages/taler-util/src/http-common.ts @@ -141,9 +141,24 @@ type ResponseOrError<T> = | { isError: false; response: T } | { isError: true; talerErrorResponse: TalerErrorResponse }; +/** + * Read Taler error details from an HTTP response. + */ export async function readTalerErrorResponse( httpResponse: HttpResponse, ): Promise<TalerErrorDetail> { + const contentType = httpResponse.headers.get("content-type"); + if (contentType !== "application/json") { + throw TalerError.fromDetail( + TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE, + { + requestUrl: httpResponse.requestUrl, + requestMethod: httpResponse.requestMethod, + httpStatusCode: httpResponse.status, + }, + "Error response did not even contain JSON. The request URL might be wrong or the service might be unavailable.", + ); + } let errJson; try { errJson = await httpResponse.json(); |