Skip to content

Commit 22bd8ec

Browse files
petyosiCopilot
andauthored
Capture structured Error.cause data (#148)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent ce583e1 commit 22bd8ec

8 files changed

Lines changed: 582 additions & 26 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"logfire": patch
3+
---
4+
5+
Add npm bugs metadata for the Logfire package.

.changeset/tame-errors-cause.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'logfire': patch
3+
---
4+
5+
Include `Error.cause` chains in recorded exception stacktraces and as structured `exception.cause` attributes.

examples/express/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Used for reporting traces to Logfire
22
# Change the URL if you're using a different Logfire instance
33
# LOGFIRE_BASE_URL=https://logfire-api.pydantic.dev/
4-
LOGFIRE_WRITE_TOKEN=your-write-token
4+
LOGFIRE_TOKEN=your-write-token
55
EXPRESS_PORT=8080

examples/express/app.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@ import * as logfire from '@pydantic/logfire-node'
55
const PORT: number = parseInt(process.env.EXPRESS_PORT || '8080')
66
const app: Express = express()
77

8+
function createStructuredCauseDemoError(): Error {
9+
const databaseCause = Object.assign(new Error('database connection rejected'), {
10+
attempt: 3,
11+
details: {
12+
host: 'primary-db.internal',
13+
pool: 'checkout-writes',
14+
retryable: true,
15+
},
16+
statusCode: 503,
17+
})
18+
19+
const serviceCause = Object.assign(new Error('checkout storage call failed', { cause: databaseCause }), {
20+
operation: 'checkout.create_order',
21+
requestId: 'demo-request-structured-cause',
22+
})
23+
24+
return Object.assign(new Error('structured Error.cause demo failed', { cause: serviceCause }), {
25+
cartId: 'cart_demo_123',
26+
customerTier: 'enterprise',
27+
})
28+
}
29+
830
function getRandomNumber(min: number, max: number) {
931
return Math.floor(Math.random() * (max - min) + min)
1032
}
@@ -24,13 +46,21 @@ app.get('/rolldice', (req, res) => {
2446
res.send(getRandomNumber(1, 6).toString())
2547
})
2648

49+
app.get('/structured-cause-error', (_req, _res) => {
50+
throw createStructuredCauseDemoError()
51+
})
52+
2753
// Report an error to Logfire, using the Express error handler.
2854
app.use((err: Error, _req: Request, res: Response, _next: () => unknown) => {
29-
logfire.reportError(err.message, err)
55+
logfire.reportError(err.message, err, {
56+
demo: 'structured-error-cause',
57+
expectedAttributes: ['exception.cause', 'logfire.json_schema'],
58+
})
3059
res.status(500)
31-
res.send('An error occured')
60+
res.send('An error occurred')
3261
})
3362

3463
app.listen(PORT, () => {
3564
console.log(`Listening for requests on http://localhost:${PORT}/rolldice`)
65+
console.log(`Structured cause demo: http://localhost:${PORT}/structured-cause-error`)
3666
})

examples/node/.env

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/logfire-api/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
},
1414
"sideEffects": false,
1515
"homepage": "https://pydantic.dev/logfire",
16+
"bugs": {
17+
"url": "https://github.com/pydantic/logfire-js/issues"
18+
},
1619
"license": "MIT",
1720
"publishConfig": {
1821
"access": "public"

0 commit comments

Comments
 (0)