Skip to content

Commit

Permalink
fix(errors): not all errors were being wrapped
Browse files Browse the repository at this point in the history
  • Loading branch information
tompahoward committed Jul 4, 2022
1 parent abb23b2 commit 37420cc
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions src/waychaser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ export async function _waychaser<Content>(
* @param uriOrRequest
* @param defaultOptions
* @param options
* @returns WayChaserResponse<Content>
* @throws TypeError | AbortError | WayChaserResponseProblem
* @returns {Promise<WayChaserResponse<Content>>}
* @throws {TypeError | AbortError | WayChaserProblem}
*/
export async function _waychaser<Content>(
uriOrRequest: string | Request,
Expand Down Expand Up @@ -461,39 +461,45 @@ export async function _waychaser<Content>(
)

try {
try {
// TODO allow lazy loading of the content
const content = await mergedOptions.contentParser(baseResponse)
// content is
// 1. unknown,
// 4. undefined
const response = WayChaserResponse.create(baseResponse, content, defaultOptions, mergedOptions)
// TODO allow lazy loading of the content
const content = await mergedOptions.contentParser(baseResponse)
// content is
// 1. unknown,
// 4. undefined
const response = WayChaserResponse.create(baseResponse, content, defaultOptions, mergedOptions)

stop = false
for (const interceptor of updateOptions.postInterceptors) {
interceptor(response, () => (stop = true))
if (stop) {
break
}
}
return response
}
catch(error){
// for some reason, error instanceof ProblemDocument was returning false
throw error.constructor.name === 'ProblemDocument' ? new WayChaserProblem({
response: new WayChaserResponse({ handlers: mergedOptions.defaultHandlers, defaultOptions, baseResponse, content: error, parameters: mergedOptions.parameters }),
problem: error,
}) : error;
}
} catch(error) {
stop = false
for (const interceptor of updateOptions.postErrorInterceptors) {
interceptor(error, () => (stop = true))
for (const interceptor of updateOptions.postInterceptors) {
interceptor(response, () => (stop = true))
if (stop) {
break
}
}
throw error
return response
}
catch(error: unknown){
const response = new WayChaserResponse({ handlers: mergedOptions.defaultHandlers, defaultOptions, baseResponse, content: error, parameters: mergedOptions.parameters })

const wayChaserProblem = error instanceof ProblemDocument ? new WayChaserProblem({
response,
problem: error,
}) : new WayChaserProblem({
response,
problem: new ProblemDocument({
type: "https://waychaser.io/unexected-error",
title: "An unexpected error occurred",
error
}),
});
stop = false
for (const interceptor of updateOptions.postErrorInterceptors) {
interceptor(wayChaserProblem, () => (stop = true))
if (stop) {
break
}
}

throw wayChaserProblem
}
}

Expand Down

0 comments on commit 37420cc

Please sign in to comment.