github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/webapp/javascript/util/handleError.ts (about) 1 import type { RequestError, RequestNotOkError } from '@webapp/services/base'; 2 import { ZodError } from 'zod'; 3 import { addNotification } from '@webapp/redux/reducers/notifications'; 4 import { useAppDispatch } from '@webapp/redux/hooks'; 5 6 /** 7 * handleError handles service errors 8 */ 9 export default async function handleError( 10 dispatch: ReturnType<typeof useAppDispatch>, 11 message: string, 12 error: ZodError | RequestError 13 ): Promise<void> { 14 // We log the error in case a tech-savy user wants to debug themselves 15 console.error(error); 16 17 let errorMessage; 18 if ('message' in error) { 19 errorMessage = error.message; 20 } 21 22 // a ZodError means its format is not what we expect 23 if (error instanceof ZodError) { 24 errorMessage = 'response not in the expected format'; 25 } 26 27 // display a notification 28 dispatch( 29 addNotification({ 30 title: 'Error', 31 message: [message, errorMessage].join('\n'), 32 type: 'danger', 33 }) 34 ); 35 }