Skip to content

Commit

Permalink
fix: throwing an error inside resolveStatus caused an uncaught error
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Dec 13, 2022
1 parent c0f8a26 commit a2f59a1
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions lib/core/api/resolve-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { InternalConfiguration } from "../../exportable-types";
import type { ResolvableEntity } from "../../types";

import { environment } from "../../environment";
import { UrbexError } from "../error";
Expand All @@ -15,24 +16,35 @@ export function resolveRequest(
this: ResolvableBindings,
resolve: Resolve,
reject: Reject,
entity: any
entity: ResolvableEntity
): void {
const status = environment.isNode ? entity.response.statusCode : entity.response.status;
const canResolve = this.config.resolveStatus(this.config, status);
const errorInstance: UrbexError = UrbexError.createErrorInstance.call(this, UrbexError);

if (canResolve) {
return resolve(entity);
}
errorInstance.status = status;
errorInstance.response = entity.response;
errorInstance.request = this.request;

try {
const canResolve = this.config.resolveStatus(this.config, status);

const error: UrbexError = UrbexError.createErrorInstance.call(this, UrbexError);
const errorMessage = `Request failed with status code ${status}`;
if (canResolve) {
return resolve(entity);
}

error.message = environment.isNode
? entity.response.statusMessage
: entity.response.statusText ?? errorMessage;
error.request = this.request;
error.status = status;
error.response = entity.response;
if (environment.isNode) {
errorInstance.message = entity.response.statusMessage;
} else {
errorInstance.message = entity.response.statusText;
}

return reject(error);
if (!errorInstance.message) {
errorInstance.message = `Request failed with status code ${status}`;
}

return reject(errorInstance);
} catch (error) {
errorInstance.message = error.message;
return reject(errorInstance);
}
}

0 comments on commit a2f59a1

Please sign in to comment.