@@ -44,18 +44,15 @@ async function findAddresses(req, res) {
4444 Accept : 'text/plain' ,
4545 'Content-Type' : 'application/json' ,
4646 } ;
47- const response = await axios . get ( url , headers ) ;
48- if ( response . data [ 0 ] ?. Error ) {
49- const errorObj = response . data [ 0 ] ;
50- log . error ( 'Canada Post address object contains an error' , {
51- searchTerm : req ?. query ?. searchTerm ,
52- errorCode : errorObj . Error ,
53- description : errorObj . Description ,
54- cause : errorObj . Cause ,
55- } ) ;
56- throw new Error ( `Canada Post error: ${ errorObj . Description } ` ) ;
57- }
47+ const response = await axios . get ( url , { headers } ) ;
48+ if ( Array . isArray ( response . data ) ) {
49+ const errorObj = response . data . find ( ( item ) => item ?. Error ) ;
5850
51+ if ( errorObj ) {
52+ log . error ( 'Canada Post address object contains an error' , errorObj ) ;
53+ throw new Error ( 'Canada Post error' ) ;
54+ }
55+ }
5956 if ( req ?. query ?. searchTerm && Redis . isReady ) {
6057 Redis . client . json . set ( REDIS_MAP , `$.${ Redis . encodeKey ( req ?. query ?. searchTerm ) } ` , response . data ) ;
6158 Redis . client . expire ( REDIS_MAP , ...REDIS_EXPIRE_ARGS ) ;
@@ -68,11 +65,8 @@ async function findAddresses(req, res) {
6865 searchTerm : req ?. query ?. searchTerm ,
6966 error : e . message ,
7067 } ) ;
71-
72- return res . status ( HttpStatus . BAD_REQUEST ) . json ( {
73- success : false ,
74- message : "We couldn't verify that address. Please check the spelling or enter it manually." ,
75- isError : true ,
68+ return res . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . json ( {
69+ message : 'Address lookup failed. Please try again later.' ,
7670 } ) ;
7771 }
7872}
0 commit comments