Skip to content

Commit

Permalink
Add middleware for error handling in Toolshed API client
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Jul 12, 2024
1 parent cb3499c commit 6557716
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/tool_shed/webapp/frontend/src/schema/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import createClient from "openapi-fetch"
import createClient, { Middleware } from "openapi-fetch"
import type { paths as ToolShedApiPaths } from "./schema"

const errorHandlingMiddleware: Middleware = {
async onResponse({ response }) {
if (!response.ok) {
const errorMessage = response.headers.get("content-type")?.includes("json")
? await response.clone().json()
: await response.clone().text()
throw new Error(`Request failed with status ${response.status}`, {
cause: errorMessage,
})
}
},
}

const client = createClient<ToolShedApiPaths>({ baseUrl: "" })
client.use(errorHandlingMiddleware)

export { type ToolShedApiPaths, client }

0 comments on commit 6557716

Please sign in to comment.