@@ -422,5 +422,254 @@ describe("lib/axios-logger: ", () => {
422422 }
423423 }
424424 } ) ;
425+
426+ it ( "returns identifier when error object has no messages but has identifier" , ( ) => {
427+ const responseData = {
428+ errors : [
429+ {
430+ email : "test@example.com" ,
431+ // No errors property, just identifier
432+ } ,
433+ ] ,
434+ } ;
435+ // @ts -ignore
436+ const axiosError = new AxiosError (
437+ "Request failed" ,
438+ "422" ,
439+ { headers : { } } as any ,
440+ {
441+ data : responseData ,
442+ status : 422 ,
443+ }
444+ ) ;
445+ axiosError . response = {
446+ data : responseData ,
447+ status : 422 ,
448+ statusText : "Unprocessable Entity" ,
449+ headers : { } ,
450+ config : { } as any ,
451+ } ;
452+
453+ expect . assertions ( 2 ) ;
454+
455+ try {
456+ axiosLogger ( axiosError ) ;
457+ } catch ( error ) {
458+ expect ( error ) . toBeInstanceOf ( MailtrapError ) ;
459+ if ( error instanceof MailtrapError ) {
460+ expect ( error . message ) . toBe ( "test@example.com" ) ;
461+ }
462+ }
463+ } ) ;
464+
465+ it ( "returns default message when data is not an object" , ( ) => {
466+ const responseData = "string data" ;
467+ // @ts -ignore
468+ const axiosError = new AxiosError (
469+ "Network error" ,
470+ "500" ,
471+ { headers : { } } as any ,
472+ {
473+ data : responseData ,
474+ status : 500 ,
475+ }
476+ ) ;
477+ axiosError . response = {
478+ data : responseData ,
479+ status : 500 ,
480+ statusText : "Internal Server Error" ,
481+ headers : { } ,
482+ config : { } as any ,
483+ } ;
484+
485+ expect . assertions ( 2 ) ;
486+
487+ try {
488+ axiosLogger ( axiosError ) ;
489+ } catch ( error ) {
490+ expect ( error ) . toBeInstanceOf ( MailtrapError ) ;
491+ if ( error instanceof MailtrapError ) {
492+ expect ( error . message ) . toBe ( "Network error" ) ;
493+ }
494+ }
495+ } ) ;
496+
497+ it ( "returns default message when data is null" , ( ) => {
498+ const responseData = null ;
499+ // @ts -ignore
500+ const axiosError = new AxiosError (
501+ "Network error" ,
502+ "500" ,
503+ { headers : { } } as any ,
504+ {
505+ data : responseData ,
506+ status : 500 ,
507+ }
508+ ) ;
509+ axiosError . response = {
510+ data : responseData ,
511+ status : 500 ,
512+ statusText : "Internal Server Error" ,
513+ headers : { } ,
514+ config : { } as any ,
515+ } ;
516+
517+ expect . assertions ( 2 ) ;
518+
519+ try {
520+ axiosLogger ( axiosError ) ;
521+ } catch ( error ) {
522+ expect ( error ) . toBeInstanceOf ( MailtrapError ) ;
523+ if ( error instanceof MailtrapError ) {
524+ expect ( error . message ) . toBe ( "Network error" ) ;
525+ }
526+ }
527+ } ) ;
528+
529+ it ( "returns default message when response data has no error or errors" , ( ) => {
530+ const responseData = { success : true } ;
531+ // @ts -ignore
532+ const axiosError = new AxiosError (
533+ "Request failed" ,
534+ "200" ,
535+ { headers : { } } as any ,
536+ {
537+ data : responseData ,
538+ status : 200 ,
539+ }
540+ ) ;
541+ axiosError . response = {
542+ data : responseData ,
543+ status : 200 ,
544+ statusText : "OK" ,
545+ headers : { } ,
546+ config : { } as any ,
547+ } ;
548+
549+ expect . assertions ( 2 ) ;
550+
551+ try {
552+ axiosLogger ( axiosError ) ;
553+ } catch ( error ) {
554+ expect ( error ) . toBeInstanceOf ( MailtrapError ) ;
555+ if ( error instanceof MailtrapError ) {
556+ expect ( error . message ) . toBe ( "Request failed" ) ;
557+ }
558+ }
559+ } ) ;
560+
561+ it ( "handles error object with no identifier and no messages" , ( ) => {
562+ const responseData = {
563+ errors : [
564+ {
565+ // No email, no id, no errors property, and someOtherField is not an array
566+ someOtherField : "value" ,
567+ } ,
568+ ] ,
569+ } ;
570+ // @ts -ignore
571+ const axiosError = new AxiosError (
572+ "Request failed" ,
573+ "422" ,
574+ { headers : { } } as any ,
575+ {
576+ data : responseData ,
577+ status : 422 ,
578+ }
579+ ) ;
580+ axiosError . response = {
581+ data : responseData ,
582+ status : 422 ,
583+ statusText : "Unprocessable Entity" ,
584+ headers : { } ,
585+ config : { } as any ,
586+ } ;
587+
588+ expect . assertions ( 2 ) ;
589+
590+ try {
591+ axiosLogger ( axiosError ) ;
592+ } catch ( error ) {
593+ expect ( error ) . toBeInstanceOf ( MailtrapError ) ;
594+ if ( error instanceof MailtrapError ) {
595+ // When all error objects return null, extractMessagesFromErrorObjects returns empty string ""
596+ // Empty string is falsy, so if (extracted) fails and falls through to stringify
597+ // Since errors is an array, typeof errors === "object" is true, so it stringifies
598+ expect ( error . message ) . toBe ( "[object Object]" ) ;
599+ }
600+ }
601+ } ) ;
602+
603+ it ( "handles errors object that doesn't match any pattern" , ( ) => {
604+ const responseData = {
605+ errors : {
606+ // Not name/base, and not an array of field errors
607+ someField : "not an array" ,
608+ } ,
609+ } ;
610+ // @ts -ignore
611+ const axiosError = new AxiosError (
612+ "Request failed" ,
613+ "422" ,
614+ { headers : { } } as any ,
615+ {
616+ data : responseData ,
617+ status : 422 ,
618+ }
619+ ) ;
620+ axiosError . response = {
621+ data : responseData ,
622+ status : 422 ,
623+ statusText : "Unprocessable Entity" ,
624+ headers : { } ,
625+ config : { } as any ,
626+ } ;
627+
628+ expect . assertions ( 2 ) ;
629+
630+ try {
631+ axiosLogger ( axiosError ) ;
632+ } catch ( error ) {
633+ expect ( error ) . toBeInstanceOf ( MailtrapError ) ;
634+ if ( error instanceof MailtrapError ) {
635+ // Should stringify the errors object
636+ expect ( error . message ) . toBe ( "[object Object]" ) ;
637+ }
638+ }
639+ } ) ;
640+
641+ it ( "handles errors as non-array, non-object value" , ( ) => {
642+ const responseData = {
643+ errors : "string error" ,
644+ } ;
645+ // @ts -ignore
646+ const axiosError = new AxiosError (
647+ "Request failed" ,
648+ "422" ,
649+ { headers : { } } as any ,
650+ {
651+ data : responseData ,
652+ status : 422 ,
653+ }
654+ ) ;
655+ axiosError . response = {
656+ data : responseData ,
657+ status : 422 ,
658+ statusText : "Unprocessable Entity" ,
659+ headers : { } ,
660+ config : { } as any ,
661+ } ;
662+
663+ expect . assertions ( 2 ) ;
664+
665+ try {
666+ axiosLogger ( axiosError ) ;
667+ } catch ( error ) {
668+ expect ( error ) . toBeInstanceOf ( MailtrapError ) ;
669+ if ( error instanceof MailtrapError ) {
670+ expect ( error . message ) . toBe ( "string error" ) ;
671+ }
672+ }
673+ } ) ;
425674 } ) ;
426675} ) ;
0 commit comments