Skip to content

Commit

Permalink
add request logger
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Nov 5, 2023
1 parent b528ae1 commit 4cfff2e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ export const createHandler = ({
fetch = globalThis.fetch,
log = console.log
}) => (req, res) => {
handler(req, res, apiKey, fetch).catch(err => {
log(err)
Sentry.captureException(err)
res.statusCode = 500
res.end('Internal Server Error')
})
const start = new Date()
log(`${req.method} ${req.url} ...`)
handler(req, res, apiKey, fetch)
.catch(err => {
log(err)
Sentry.captureException(err)
res.statusCode = 500
res.end('Internal Server Error')
})
.then(() => {
log(`${req.method} ${req.url} ${res.statusCode} (${new Date() - start}ms)`)
})
}
6 changes: 4 additions & 2 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ describe('Unit tests', () => {
status: 200,
json: async () => ({ identifications: [] })
}
}
},
log () {}
}))

const { status } = await fetch(`http://127.0.0.1:${port}/0xADDRESS`)
Expand All @@ -52,7 +53,8 @@ describe('Unit tests', () => {
status: 200,
json: async () => ({ identifications: [{}] })
}
}
},
log () {}
}))

const { status } = await fetch(`http://127.0.0.1:${port}/0xADDRESS`)
Expand Down

0 comments on commit 4cfff2e

Please sign in to comment.