mirror of
https://github.com/MetaFam/TheGame.git
synced 2026-01-13 08:37:53 -05:00
58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
import { useToast } from '@metafam/ds';
|
|
import { useCallback } from 'react';
|
|
import { CombinedError } from 'urql';
|
|
|
|
import { CONFIG } from '#config';
|
|
|
|
export const errorHandler = (error: Error | CombinedError): void => {
|
|
// eslint-disable-next-line no-console
|
|
console.info(`In errorHandler: ${CONFIG.appEnv}`);
|
|
console.error({ errorHandler: error });
|
|
};
|
|
|
|
type DebugErrorReports = {
|
|
debugErrorReports: () => void;
|
|
};
|
|
|
|
/**
|
|
* @description A hook to test our error reporting service. Currently [Honeybadger](https://app.honeybadger.io/projects/104891/faults?q=-is%3Aresolved+-is%3Aignored&sort=last_seen_desc)
|
|
* @returns `debugErrorReport` - Function to trigger an error to be reported
|
|
* based on `urlParams`: `debug=unhandledError` or `debug=handledError`.
|
|
*/
|
|
export const useDebugErrorReports = (): DebugErrorReports => {
|
|
const toast = useToast();
|
|
|
|
const debugErrorReports = useCallback(() => {
|
|
if (typeof window !== 'undefined') {
|
|
const query = window.location.search;
|
|
const urlParams = new URLSearchParams(query);
|
|
const throwUnhandledError = urlParams.get('debug') === 'unhandledError';
|
|
const throwHandledError = urlParams.get('debug') === 'handledError';
|
|
const errorMessage = urlParams.get('message');
|
|
|
|
if (throwUnhandledError) {
|
|
console.error(errorMessage ?? 'Debug Unhandled error');
|
|
throw new Error(errorMessage ?? 'Debug Unhandled error');
|
|
}
|
|
|
|
if (throwHandledError) {
|
|
try {
|
|
console.error(errorMessage ?? 'Debug Handled error');
|
|
throw new Error(errorMessage ?? 'Debug Handled Error');
|
|
} catch (err) {
|
|
const error = err as Error;
|
|
toast({
|
|
title: 'Error Debug',
|
|
description: `Throwing handled error: "${error.message}".`,
|
|
status: 'error',
|
|
isClosable: true,
|
|
});
|
|
errorHandler(error);
|
|
}
|
|
}
|
|
}
|
|
}, [toast]);
|
|
|
|
return { debugErrorReports };
|
|
};
|