Skip to content

Commit

Permalink
don't log errors from API by default and change error levels
Browse files Browse the repository at this point in the history
  • Loading branch information
tom2drum committed Nov 18, 2024
1 parent 8339d37 commit 742e381
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 11 deletions.
5 changes: 3 additions & 2 deletions lib/api/useApiFetch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Params<R extends ResourceName> {
pathParams?: ResourcePathParams<R>;
queryParams?: Record<string, string | Array<string> | number | boolean | undefined | null>;
fetchParams?: Pick<FetchParams, 'body' | 'method' | 'signal' | 'headers'>;
logError?: boolean;
}

export default function useApiFetch() {
Expand All @@ -30,7 +31,7 @@ export default function useApiFetch() {

return React.useCallback(<R extends ResourceName, SuccessType = unknown, ErrorType = unknown>(
resourceName: R,
{ pathParams, queryParams, fetchParams }: Params<R> = {},
{ pathParams, queryParams, fetchParams, logError }: Params<R> = {},
) => {
const apiToken = cookies.get(cookies.NAMES.API_TOKEN);

Expand Down Expand Up @@ -58,7 +59,7 @@ export default function useApiFetch() {
},
{
resource: resource.path,
omitMonitoringLog: true, // disable logging of API errors to Rollbar
logError,
},
);
}, [ fetch, csrfToken ]);
Expand Down
5 changes: 3 additions & 2 deletions lib/api/useApiQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Params<R extends ResourceName, E = unknown, D = ResourcePayload
queryParams?: Record<string, string | Array<string> | number | boolean | undefined>;
fetchParams?: Pick<FetchParams, 'body' | 'method' | 'headers'>;
queryOptions?: Partial<Omit<UseQueryOptions<ResourcePayload<R>, ResourceError<E>, D>, 'queryFn'>>;
logError?: boolean;
}

export function getResourceKey<R extends ResourceName>(resource: R, { pathParams, queryParams }: Params<R> = {}) {
Expand All @@ -23,7 +24,7 @@ export function getResourceKey<R extends ResourceName>(resource: R, { pathParams

export default function useApiQuery<R extends ResourceName, E = unknown, D = ResourcePayload<R>>(
resource: R,
{ queryOptions, pathParams, queryParams, fetchParams }: Params<R, E, D> = {},
{ queryOptions, pathParams, queryParams, fetchParams, logError }: Params<R, E, D> = {},
) {
const apiFetch = useApiFetch();

Expand All @@ -33,7 +34,7 @@ export default function useApiQuery<R extends ResourceName, E = unknown, D = Res
// all errors and error typing is handled by react-query
// so error response will never go to the data
// that's why we are safe here to do type conversion "as Promise<ResourcePayload<R>>"
return apiFetch(resource, { pathParams, queryParams, fetchParams: { ...fetchParams, signal } }) as Promise<ResourcePayload<R>>;
return apiFetch(resource, { pathParams, queryParams, logError, fetchParams: { ...fetchParams, signal } }) as Promise<ResourcePayload<R>>;
},
...queryOptions,
});
Expand Down
2 changes: 1 addition & 1 deletion lib/hooks/useAddressProfileApiQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function useAddressProfileApiQuery(hash: string | undefined, isEn
return Promise.reject();
}

return fetch(feature.apiUrlTemplate.replace('{address}', hash), undefined, { omitMonitoringLog: true });
return fetch(feature.apiUrlTemplate.replace('{address}', hash));
},
enabled: isEnabled && Boolean(hash),
refetchOnMount: false,
Expand Down
4 changes: 2 additions & 2 deletions lib/hooks/useFetch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface Params {

interface Meta {
resource?: ResourcePath;
omitMonitoringLog?: boolean;
logError?: boolean;
}

export default function useFetch() {
Expand Down Expand Up @@ -53,7 +53,7 @@ export default function useFetch() {
statusText: response.statusText,
};

if (!meta?.omitMonitoringLog && rollbar) {
if (meta?.logError && rollbar) {
rollbar.warn('Client fetch failed', {
resource: meta?.resource,
status_code: error.status,
Expand Down
2 changes: 1 addition & 1 deletion lib/hooks/useIsSafeAddress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function useIsSafeAddress(hash: string | undefined): boolean {
return Promise.reject();
}

return fetch(`${ feature.apiUrl }/${ hash }`, undefined, { omitMonitoringLog: true });
return fetch(`${ feature.apiUrl }/${ hash }`);
},
enabled: feature.isEnabled && Boolean(hash),
refetchOnMount: false,
Expand Down
2 changes: 1 addition & 1 deletion ui/address/details/AddressQrCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const AddressQrCode = ({ address, className, isLoading }: Props) => {
QRCode.toString(address.hash, SVG_OPTIONS, (error: Error | null | undefined, svg: string) => {
if (error) {
setError('We were unable to generate QR code.');
rollbar?.error('QR code generation failed');
rollbar?.warn('QR code generation failed');
return;
}

Expand Down
1 change: 1 addition & 0 deletions ui/address/utils/useAddressQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default function useAddressQuery({ hash, isEnabled = true }: Params): Add
return isRefetchEnabled ? 15 * SECOND : false;
},
},
logError: true,
});

const rpcQuery = useQuery<RpcResponseType, unknown, Address | null>({
Expand Down
12 changes: 11 additions & 1 deletion ui/shared/AppError/AppErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { chakra } from '@chakra-ui/react';
import React from 'react';

import getErrorCauseStatusCode from 'lib/errors/getErrorCauseStatusCode';
import { useRollbar } from 'lib/rollbar';
import ErrorBoundary from 'ui/shared/ErrorBoundary';

Expand All @@ -25,7 +26,16 @@ const AppErrorBoundary = ({ className, children, Container }: Props) => {
}, [ className, Container ]);

const handleError = React.useCallback((error: Error) => {
rollbar?.error(error);
const statusCode = getErrorCauseStatusCode(error);
if (statusCode || !rollbar) {
// For now, we are not interested in logging errors from the API.
// If an error from a resource should be logged, please consider passing "logError: true" to the useApiQuery or useApiFetch hook.
return;
}

// To this point, there can only be errors that lead to a page crash.
// Therefore, we set the error level to "critical."
rollbar.critical(error);
}, [ rollbar ]);

return (
Expand Down
1 change: 0 additions & 1 deletion ui/snippets/auth/useRedirectForInvalidAuthToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default function useRedirectForInvalidAuthToken() {
const apiToken = cookies.get(cookies.NAMES.API_TOKEN);

if (apiToken) {
rollbar?.warn('Invalid API token');
cookies.remove(cookies.NAMES.API_TOKEN);
window.location.assign('/');
}
Expand Down

0 comments on commit 742e381

Please sign in to comment.