@@ -102,8 +102,8 @@ export default {
102102 }
103103 catch ( error : any ) {
104104 if ( error instanceof Error ) {
105- if ( error . message . startsWith ( "D1_" ) && error . cause instanceof Error ) {
106- context . waitUntil ( triggerAlarm ( env , "D1 Error Alarm" , `An error was thrown by D1 during execution.\n \n\`\`\`\n${ error . message } \n\`\`\`\`\`\`\n ${ error . cause . message } \n\`\`\`\ n${ request . method } ${ request . url } \nRemote Address: || ${ request . headers . get ( "CF-Connecting-IP" ) } ||` ) ) ;
105+ if ( error . message . startsWith ( "D1_" ) ) {
106+ context . waitUntil ( triggerAlarm ( env , "D1 Error Alarm" , `An error was thrown by D1 during execution.\n \n\`\`\`\n${ error . message } \n\`\`\`\n${ request . method } ${ request . url } \nRemote Address: || ${ request . headers . get ( "CF-Connecting-IP" ) } ||` ) ) ;
107107
108108 return new Response ( undefined , {
109109 status : 502 ,
@@ -132,85 +132,106 @@ export class ActivityDurableObject {
132132 } ;
133133
134134 async fetch ( request : Request ) {
135- const { activityId } = await request . json ( ) as {
136- activityId ?: string ;
137- } ;
135+ try {
136+ const { activityId } = await request . json ( ) as {
137+ activityId ?: string ;
138+ } ;
138139
139- if ( ! activityId )
140- return Response . json ( { success : false } ) ;
140+ if ( ! activityId )
141+ return Response . json ( { success : false } ) ;
141142
142- const activity = await getActivityById ( this . env . DATABASE , activityId ) ;
143+ const activity = await getActivityById ( this . env . DATABASE , activityId ) ;
143144
144- if ( ! activity )
145- return Response . json ( { success : false } ) ;
145+ if ( ! activity )
146+ return Response . json ( { success : false } ) ;
146147
147- if ( await getActivitySummaryById ( this . env . DATABASE , activity . id ) )
148- return Response . json ( { success : true } ) ;
148+ if ( await getActivitySummaryById ( this . env . DATABASE , activity . id ) )
149+ return Response . json ( { success : true } ) ;
149150
150- const bucket = await this . env . BUCKET . get ( `activities/${ activity . id } .json` ) ;
151+ const bucket = await this . env . BUCKET . get ( `activities/${ activity . id } .json` ) ;
151152
152- if ( ! bucket )
153- return Response . json ( { success : false } ) ;
153+ if ( ! bucket )
154+ return Response . json ( { success : false } ) ;
154155
155- const sessions = await bucket . json < Array < any > > ( ) ;
156+ const sessions = await bucket . json < Array < any > > ( ) ;
156157
157- let startArea = null ;
158- let finishArea = null ;
159- let distance = 0 ;
160- let elevation = 0 ;
161- let maxSpeed = 0 ;
158+ let startArea = null ;
159+ let finishArea = null ;
160+ let distance = 0 ;
161+ let elevation = 0 ;
162+ let maxSpeed = 0 ;
162163
163- if ( sessions . length && sessions [ 0 ] . locations . length ) {
164- const getAreaName = async ( coords : any ) => {
165- const geocoding = await getReverseGeocoding ( this . env . GOOGLE_MAPS_API_TOKEN , coords . latitude , coords . longitude ) ;
166-
167- if ( geocoding . results . length ) {
168- const geocodingResult = geocoding . results [ 0 ] ;
169-
170- const geocodingComponent = geocodingResult . address_components . find ( ( component : any ) => component . types . includes ( "postal_town" ) ) ?? geocodingResult . address_components . find ( ( component : any ) => component . types . includes ( "political" ) ) ?? geocodingResult . address_components . find ( ( component : any ) => component . types . includes ( "country" ) ) ;
171-
172- return geocodingComponent ?. long_name ?? null ;
173- }
164+ if ( sessions . length && sessions [ 0 ] . locations . length ) {
165+ const getAreaName = async ( coords : any ) => {
166+ const geocoding = await getReverseGeocoding ( this . env . GOOGLE_MAPS_API_TOKEN , coords . latitude , coords . longitude ) ;
167+
168+ if ( geocoding . results . length ) {
169+ const geocodingResult = geocoding . results [ 0 ] ;
170+
171+ const geocodingComponent = geocodingResult . address_components . find ( ( component : any ) => component . types . includes ( "postal_town" ) ) ?? geocodingResult . address_components . find ( ( component : any ) => component . types . includes ( "political" ) ) ?? geocodingResult . address_components . find ( ( component : any ) => component . types . includes ( "country" ) ) ;
172+
173+ return geocodingComponent ?. long_name ?? null ;
174+ }
174175
175- return null ;
176- }
176+ return null ;
177+ }
177178
178- await Promise . all ( [
179- getAreaName ( sessions [ 0 ] . locations [ 0 ] . coords ) . then ( ( name ) => startArea = name ) ,
180- getAreaName ( sessions [ sessions . length - 1 ] . locations [ sessions [ sessions . length - 1 ] . locations . length - 1 ] . coords ) . then ( ( name ) => finishArea = name )
181- ] ) ;
182- }
179+ await Promise . all ( [
180+ getAreaName ( sessions [ 0 ] . locations [ 0 ] . coords ) . then ( ( name ) => startArea = name ) ,
181+ getAreaName ( sessions [ sessions . length - 1 ] . locations [ sessions [ sessions . length - 1 ] . locations . length - 1 ] . coords ) . then ( ( name ) => finishArea = name )
182+ ] ) ;
183+ }
183184
184- const speeds = [ ] ;
185+ const speeds = [ ] ;
185186
186- for ( let session of sessions ) {
187- for ( let index = 1 ; index < session . locations . length ; index ++ ) {
188- distance += getDistance ( session . locations [ index - 1 ] . coords , session . locations [ index ] . coords , 1 ) ;
187+ for ( let session of sessions ) {
188+ for ( let index = 1 ; index < session . locations . length ; index ++ ) {
189+ distance += getDistance ( session . locations [ index - 1 ] . coords , session . locations [ index ] . coords , 1 ) ;
189190
190- speeds . push ( session . locations [ index ] . coords . speed ) ;
191+ speeds . push ( session . locations [ index ] . coords . speed ) ;
191192
192- elevation += Math . max ( 0 , session . locations [ index ] . coords . altitude - session . locations [ index - 1 ] . coords . altitude ) ;
193+ elevation += Math . max ( 0 , session . locations [ index ] . coords . altitude - session . locations [ index - 1 ] . coords . altitude ) ;
193194
194- if ( session . locations [ index ] . coords . speed > maxSpeed )
195- maxSpeed = session . locations [ index ] . coords . speed ;
195+ if ( session . locations [ index ] . coords . speed > maxSpeed )
196+ maxSpeed = session . locations [ index ] . coords . speed ;
197+ }
196198 }
197- }
198199
199- const speedSum = speeds . reduce ( ( a , b ) => a + b , 0 ) ;
200- const averageSpeed = ( speedSum / speeds . length ) || 0 ;
200+ const speedSum = speeds . reduce ( ( a , b ) => a + b , 0 ) ;
201+ const averageSpeed = ( speedSum / speeds . length ) || 0 ;
201202
202- await Promise . allSettled ( [
203- createActivitySummary ( this . env . DATABASE , activity . id , "distance" , distance ) ,
204- createActivitySummary ( this . env . DATABASE , activity . id , "average_speed" , averageSpeed ) ,
205- createActivitySummary ( this . env . DATABASE , activity . id , "elevation" , elevation ) ,
206- createActivitySummary ( this . env . DATABASE , activity . id , "max_speed" , maxSpeed ) ,
207- updateActivityAreas ( this . env . DATABASE , activity . id , startArea , finishArea )
208- ] ) ;
203+ await Promise . allSettled ( [
204+ createActivitySummary ( this . env . DATABASE , activity . id , "distance" , distance ) ,
205+ createActivitySummary ( this . env . DATABASE , activity . id , "average_speed" , averageSpeed ) ,
206+ createActivitySummary ( this . env . DATABASE , activity . id , "elevation" , elevation ) ,
207+ createActivitySummary ( this . env . DATABASE , activity . id , "max_speed" , maxSpeed ) ,
208+ updateActivityAreas ( this . env . DATABASE , activity . id , startArea , finishArea )
209+ ] ) ;
209210
210- await updatePersonalBestActivitySummary ( this . env . DATABASE , activity . user ) ;
211+ await updatePersonalBestActivitySummary ( this . env . DATABASE , activity . user ) ;
211212
212- return Response . json ( {
213- success : true
214- } ) ;
213+ return Response . json ( {
214+ success : true
215+ } ) ;
216+ }
217+ catch ( error : any ) {
218+ if ( error instanceof Error ) {
219+ if ( error . message . startsWith ( "D1_" ) ) {
220+ this . state . waitUntil ( triggerAlarm ( this . env , "D1 Error Alarm" , `An error was thrown by D1 during a durable object execution.\n \n\`\`\`\n${ error . message } \n\`\`\`\n${ request . method } ${ request . url } ` ) ) ;
221+
222+ return new Response ( undefined , {
223+ status : 502 ,
224+ statusText : "Bad Gateway"
225+ } ) ;
226+ }
227+ }
228+
229+ this . state . waitUntil ( triggerAlarm ( this . env , "Uncaught Error Alarm" , `An uncaught error was thrown during a durable object execution.\n \n\`\`\`\n${ error } \n\`\`\`\n${ request . method } ${ request . url } ` ) ) ;
230+
231+ return new Response ( undefined , {
232+ status : 500 ,
233+ statusText : "Internal Server Error"
234+ } ) ;
235+ }
215236 } ;
216237} ;
0 commit comments