@@ -5,6 +5,28 @@ import * as logfire from '@pydantic/logfire-node'
55const PORT : number = parseInt ( process . env . EXPRESS_PORT || '8080' )
66const 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+
830function 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.
2854app . 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
3463app . 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} )
0 commit comments