Skip to content

Commit

Permalink
PKG -- [transport-http] Add errorMessage to HTTPRequestError (#1196)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored May 25, 2022
1 parent d9bc1cc commit cd218e8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-colts-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/transport-http": minor
---

Added errorMessage property to HTTPRequestError to expose Access API errors when making requests
10 changes: 7 additions & 3 deletions packages/transport-http/src/http-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ class HTTPRequestError extends Error {
${error ? `error=${error}` : ""}
${hostname ? `hostname=${hostname}` : ""}
${path ? `path=${path}` : ""}
${port ? `port=${port}` : ""}
${method ? `method=${method}` : ""}
${requestBody ? `requestBody=${JSON.stringify(requestBody)}` : ""}
${responseBody ? `responseBody=${responseBody}` : ""}
${responseBody ? `responseBody=${JSON.stringify(responseBody)}` : ""}
${responseStatusText ? `responseStatusText=${responseStatusText}` : ""}
${reqOn ? `reqOn=${reqOn}` : ""}
${statusCode ? `statusCode=${statusCode}` : ""}
`
super(msg)
this.name = "HTTP Request Error"
this.statusCode = statusCode
this.statusCode = responseBody?.code ?? statusCode
this.errorMessage = responseBody?.message
}
}

Expand Down Expand Up @@ -105,7 +107,7 @@ export async function httpRequest({
if (res.ok) {
return res.json()
}
const responseJSON = JSON.stringify(await res.json())
const responseJSON = await res.json()
throw new HTTPRequestError({
transport: "FetchTransport",
error: responseJSON?.message,
Expand Down Expand Up @@ -204,6 +206,8 @@ export async function httpRequest({
path,
port,
method,
requestBody: body ?? null,
responseBody: responseBody,
reqOn: "end",
})
)
Expand Down

0 comments on commit cd218e8

Please sign in to comment.