@@ -37,9 +37,9 @@ export const signIn: AsyncAction<{ useExtraScopes?: boolean }> = async (
37
37
actions . userNotifications . internal . initialize ( ) ; // Seemed a bit different originally?
38
38
actions . refetchSandboxInfo ( ) ;
39
39
} catch ( error ) {
40
- actions . internal . addNotification ( {
41
- title : 'Github Authentication Error ' ,
42
- type : ' error' ,
40
+ actions . internal . handleError ( {
41
+ message : 'Could not authenticate with Github ' ,
42
+ error,
43
43
} ) ;
44
44
}
45
45
} ;
@@ -206,10 +206,11 @@ export const setCurrentSandbox: AsyncAction<Sandbox> = async (
206
206
currentModuleShortid = resolvedModule
207
207
? resolvedModule . shortid
208
208
: currentModuleShortid ;
209
- } catch ( err ) {
210
- effects . notificationToast . warning (
211
- `Could not find the module ${ sandboxOptions . currentModule } `
212
- ) ;
209
+ } catch ( error ) {
210
+ actions . internal . handleError ( {
211
+ message : `Could not find module ${ sandboxOptions . currentModule } ` ,
212
+ error,
213
+ } ) ;
213
214
}
214
215
}
215
216
@@ -275,7 +276,11 @@ export const updateCurrentSandbox: AsyncAction<Sandbox> = async (
275
276
state . editor . currentSandbox . title = sandbox . title ;
276
277
} ;
277
278
278
- export const ensurePackageJSON : AsyncAction = async ( { state, effects } ) => {
279
+ export const ensurePackageJSON : AsyncAction = async ( {
280
+ state,
281
+ actions,
282
+ effects,
283
+ } ) => {
279
284
const sandbox = state . editor . currentSandbox ;
280
285
const existingPackageJson = sandbox . modules . find (
281
286
module => module . directoryShortid == null && module . title === 'package.json'
@@ -310,6 +315,10 @@ export const ensurePackageJSON: AsyncAction = async ({ state, effects }) => {
310
315
} catch ( error ) {
311
316
sandbox . modules . splice ( sandbox . modules . indexOf ( module ) , 1 ) ;
312
317
state . editor . modulesByPath = effects . vscode . sandboxFsSync . create ( sandbox ) ;
318
+ actions . internal . handleError ( {
319
+ message : 'Could not add package.json file' ,
320
+ error,
321
+ } ) ;
313
322
}
314
323
}
315
324
} ;
@@ -330,21 +339,55 @@ export const closeTabByIndex: Action<number> = ({ state }, tabIndex) => {
330
339
state . editor . tabs . splice ( tabIndex , 1 ) ;
331
340
} ;
332
341
333
- export const onApiError : Action < ApiError > = (
334
- { state, actions, effects } ,
335
- error
336
- ) => {
337
- const { response } = error ;
342
+ export const handleError : Action < {
343
+ /*
344
+ The message that will show as title of the notification
345
+ */
346
+ message : string ;
347
+ error : ApiError | Error ;
348
+ } > = ( { actions, effects } , { message, error } ) => {
349
+ const isGenericError = ! ( 'response' in error ) || error . response . status >= 500 ;
338
350
339
- if ( response . status === 401 ) {
340
- // We need to implement a blocking modal to either sign in or refresh the browser to
341
- // continue in an anonymous state
351
+ if ( isGenericError ) {
352
+ effects . analytics . logError ( error ) ;
353
+ effects . notificationToast . add ( {
354
+ title : message ,
355
+ message : error . message ,
356
+ status : NotificationStatus . ERROR ,
357
+ } ) ;
358
+
359
+ return ;
342
360
}
343
361
344
- if ( ! response || response . status >= 500 ) {
345
- effects . analytics . logError ( error ) ;
362
+ const { response } = error as ApiError ;
363
+
364
+ if ( response . status === 401 ) {
365
+ // Reset existing sign in info
366
+ effects . jwt . reset ( ) ;
367
+ effects . analytics . setAnonymousId ( ) ;
368
+
369
+ // Allow user to sign in again in notification
370
+ effects . notificationToast . add ( {
371
+ message : 'Your session seems to be expired, please log in again...' ,
372
+ status : NotificationStatus . ERROR ,
373
+ actions : {
374
+ primary : [
375
+ {
376
+ label : 'Sign in' ,
377
+ run : ( ) => {
378
+ actions . signInClicked ( { useExtraScopes : false } ) ;
379
+ } ,
380
+ } ,
381
+ ] ,
382
+ } ,
383
+ } ) ;
384
+
385
+ return ;
346
386
}
347
387
388
+ /*
389
+ Update error message with what is coming from the server
390
+ */
348
391
const result = response . data ;
349
392
350
393
if ( result ) {
@@ -367,43 +410,31 @@ export const onApiError: Action<ApiError> = (
367
410
}
368
411
}
369
412
413
+ const notificationActions = {
414
+ primary : [ ] ,
415
+ } ;
416
+
370
417
if ( error . message . startsWith ( 'You need to sign in to create more than' ) ) {
371
418
// Error for "You need to sign in to create more than 10 sandboxes"
372
419
effects . analytics . track ( 'Anonymous Sandbox Limit Reached' , {
373
420
errorMessage : error . message ,
374
421
} ) ;
375
422
376
- effects . notificationToast . add ( {
377
- message : error . message ,
378
- status : NotificationStatus . ERROR ,
379
- actions : {
380
- primary : [
381
- {
382
- label : 'Sign in' ,
383
- run : ( ) => {
384
- actions . internal . signIn ( { } ) ;
385
- } ,
386
- } ,
387
- ] ,
423
+ notificationActions . primary . push ( {
424
+ label : 'Sign in' ,
425
+ run : ( ) => {
426
+ actions . internal . signIn ( { } ) ;
388
427
} ,
389
428
} ) ;
390
429
} else if ( error . message . startsWith ( 'You reached the maximum of' ) ) {
391
430
effects . analytics . track ( 'Non-Patron Sandbox Limit Reached' , {
392
431
errorMessage : error . message ,
393
432
} ) ;
394
433
395
- effects . notificationToast . add ( {
396
- message : error . message ,
397
- status : NotificationStatus . ERROR ,
398
- actions : {
399
- primary : [
400
- {
401
- label : 'Open Patron Page' ,
402
- run : ( ) => {
403
- window . open ( patronUrl ( ) , '_blank' ) ;
404
- } ,
405
- } ,
406
- ] ,
434
+ notificationActions . primary . push ( {
435
+ label : 'Open Patron Page' ,
436
+ run : ( ) => {
437
+ window . open ( patronUrl ( ) , '_blank' ) ;
407
438
} ,
408
439
} ) ;
409
440
} else if (
@@ -415,31 +446,28 @@ export const onApiError: Action<ApiError> = (
415
446
errorMessage : error . message ,
416
447
} ) ;
417
448
418
- effects . notificationToast . add ( {
419
- message : error . message ,
420
- status : NotificationStatus . ERROR ,
421
- actions : {
422
- primary : [
423
- {
424
- label : 'Open Patron Page' ,
425
- run : ( ) => {
426
- window . open ( patronUrl ( ) , '_blank' ) ;
427
- } ,
428
- } ,
429
- ] ,
449
+ notificationActions . primary . push ( {
450
+ label : 'Open Patron Page' ,
451
+ run : ( ) => {
452
+ window . open ( patronUrl ( ) , '_blank' ) ;
430
453
} ,
431
454
} ) ;
432
- } else {
433
- if (
434
- error . message . startsWith (
435
- 'You reached the limit of server sandboxes, we will increase the limit in the future. Please contact hello@codesandbox.io for more server sandboxes.'
436
- )
437
- ) {
438
- effects . analytics . track ( 'Patron Server Sandbox Limit Reached' , {
439
- errorMessage : error . message ,
440
- } ) ;
441
- }
442
-
443
- effects . notificationToast . error ( error . message ) ;
455
+ } else if (
456
+ error . message . startsWith (
457
+ 'You reached the limit of server sandboxes, we will increase the limit in the future. Please contact hello@codesandbox.io for more server sandboxes.'
458
+ )
459
+ ) {
460
+ effects . analytics . track ( 'Patron Server Sandbox Limit Reached' , {
461
+ errorMessage : error . message ,
462
+ } ) ;
444
463
}
464
+
465
+ effects . notificationToast . add ( {
466
+ title : message ,
467
+ message : error . message ,
468
+ status : NotificationStatus . ERROR ,
469
+ ...( notificationActions . primary . length
470
+ ? { actions : notificationActions }
471
+ : { } ) ,
472
+ } ) ;
445
473
} ;
0 commit comments