Skip to content

Commit

Permalink
fix(server): 🐛 internal error on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
apttx committed Jan 28, 2022
1 parent 07af801 commit 1b57d77
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/lib/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ const serializeResponse = async response =>
body: await response.text(),
})

const respond = (ctx, data) => {
const respond = (ctx, data, errors) => {
ctx.set('Access-Control-Allow-Origin', '*')
ctx.set('Access-Control-Allow-Methods', '*')
ctx.set('Access-Control-Allow-Headers', '*')

if (errors) {
ctx.body = {
errors,
}
return
}

const { headers = {}, body } = JSON.parse(data)

const {
Expand All @@ -34,9 +45,6 @@ const respond = (ctx, data) => {
...relevantHeaders
} = headers

ctx.set('Access-Control-Allow-Origin', '*')
ctx.set('Access-Control-Allow-Methods', '*')
ctx.set('Access-Control-Allow-Headers', '*')
for (const [header, value] of Object.entries(relevantHeaders)) {
ctx.set(header, value)
}
Expand Down Expand Up @@ -156,17 +164,12 @@ const server = config => {

ctx.status = error.status ?? 500

return respond(ctx, {
headers: { 'Content-Type': 'application/json' },
body: {
errors: [
{
message: error.message,
details: { requestBody: ctx.request.body },
},
],
return respond(ctx, null, [
{
message: error.message,
details: { requestBody: ctx.request.body },
},
})
])
}
})

Expand Down

0 comments on commit 1b57d77

Please sign in to comment.