Skip to content

Commit

Permalink
catch error in fall back to utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
archvlad committed Mar 3, 2024
1 parent 897f385 commit 57e1486
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions source/as-promise/index.ts
Expand Up @@ -7,7 +7,7 @@ import {
type RequestError,
} from '../core/errors.js';
import Request from '../core/index.js';
import {parseBody, isResponseOk, type Response} from '../core/response.js';
import {parseBody, isResponseOk, type Response, ParseError} from '../core/response.js';
import proxyEvents from '../core/utils/proxy-events.js';
import type Options from '../core/options.js';
import {CancelError, type CancelableRequest} from './types.js';
Expand Down Expand Up @@ -62,7 +62,12 @@ export default function asPromise<T>(firstRequest?: Request): CancelableRequest<
response.body = parseBody(response, options.responseType, options.parseJson, options.encoding);
} catch (error: any) {
// Fall back to `utf8`
response.body = response.rawBody.toString();
try {
response.body = response.rawBody.toString();
} catch (error) {
request._beforeError(new ParseError(error as Error, response));
return;
}

if (isResponseOk(response)) {
request._beforeError(error);
Expand Down

0 comments on commit 57e1486

Please sign in to comment.