From 79246ff8214244c46e871dd7eb06c89a19b922d1 Mon Sep 17 00:00:00 2001 From: xmflsct Date: Sun, 4 Dec 2022 14:39:27 +0100 Subject: [PATCH] Refine logging --- src/api/general.ts | 3 +- src/api/handleError.ts | 31 ----------------- src/api/helpers/index.ts | 38 ++++++++++++++++++--- src/api/instance.ts | 3 +- src/api/tooot.ts | 15 ++++---- src/screens/Compose.tsx | 5 ++- src/utils/push/useConnect.ts | 16 +++------ src/utils/slices/instances/push/register.ts | 3 +- 8 files changed, 50 insertions(+), 64 deletions(-) delete mode 100644 src/api/handleError.ts diff --git a/src/api/general.ts b/src/api/general.ts index 29e49158..09eb3966 100644 --- a/src/api/general.ts +++ b/src/api/general.ts @@ -1,6 +1,5 @@ import axios from 'axios' -import handleError, { ctx } from './handleError' -import { userAgent } from './helpers' +import { ctx, handleError, userAgent } from './helpers' export type Params = { method: 'get' | 'post' | 'put' | 'delete' diff --git a/src/api/handleError.ts b/src/api/handleError.ts deleted file mode 100644 index efbff696..00000000 --- a/src/api/handleError.ts +++ /dev/null @@ -1,31 +0,0 @@ -import chalk from 'chalk' - -export const ctx = new chalk.Instance({ level: 3 }) - -const handleError = (error: any) => { - if (error?.response) { - // The request was made and the server responded with a status code - // that falls out of the range of 2xx - console.error( - ctx.bold(' API '), - ctx.bold('response'), - error.response.status, - error?.response.data?.error || error?.response.message || 'Unknown error' - ) - return Promise.reject({ - status: error?.response.status, - message: error?.response.data?.error || error?.response.message || 'Unknown error' - }) - } else if (error?.request) { - // The request was made but no response was received - // `error.request` is an instance of XMLHttpRequest in the browser and an instance of - // http.ClientRequest in node.js - console.error(ctx.bold(' API '), ctx.bold('request'), error) - return Promise.reject() - } else { - console.error(ctx.bold(' API '), ctx.bold('internal'), error?.message) - return Promise.reject() - } -} - -export default handleError diff --git a/src/api/helpers/index.ts b/src/api/helpers/index.ts index 799b7d11..2f057967 100644 --- a/src/api/helpers/index.ts +++ b/src/api/helpers/index.ts @@ -1,6 +1,36 @@ -import Constants from "expo-constants" -import { Platform } from "react-native" +import chalk from 'chalk' +import Constants from 'expo-constants' +import { Platform } from 'react-native' -const userAgent = { 'User-Agent': `tooot/${Constants.expoConfig?.version} ${Platform.OS}/${Platform.Version}` } +const userAgent = { + 'User-Agent': `tooot/${Constants.expoConfig?.version} ${Platform.OS}/${Platform.Version}` +} -export { userAgent } \ No newline at end of file +const ctx = new chalk.Instance({ level: 3 }) +const handleError = (error: any) => { + if (error?.response) { + // The request was made and the server responded with a status code + // that falls out of the range of 2xx + console.error( + ctx.bold(' API '), + ctx.bold('response'), + error.response.status, + error?.response.data?.error || error?.response.message || 'Unknown error' + ) + return Promise.reject({ + status: error?.response.status, + message: error?.response.data?.error || error?.response.message || 'Unknown error' + }) + } else if (error?.request) { + // The request was made but no response was received + // `error.request` is an instance of XMLHttpRequest in the browser and an instance of + // http.ClientRequest in node.js + console.error(ctx.bold(' API '), ctx.bold('request'), error) + return Promise.reject() + } else { + console.error(ctx.bold(' API '), ctx.bold('internal'), error?.message) + return Promise.reject() + } +} + +export { ctx, handleError, userAgent } diff --git a/src/api/instance.ts b/src/api/instance.ts index 0126e4b1..270f88df 100644 --- a/src/api/instance.ts +++ b/src/api/instance.ts @@ -1,8 +1,7 @@ import { RootState } from '@root/store' import axios, { AxiosRequestConfig } from 'axios' import li from 'li' -import handleError, { ctx } from './handleError' -import { userAgent } from './helpers' +import { ctx, handleError, userAgent } from './helpers' export type Params = { method: 'get' | 'post' | 'put' | 'delete' | 'patch' diff --git a/src/api/tooot.ts b/src/api/tooot.ts index 94f2bb98..bfe6d8e3 100644 --- a/src/api/tooot.ts +++ b/src/api/tooot.ts @@ -1,8 +1,7 @@ import * as Sentry from '@sentry/react-native' import { mapEnvironment } from '@utils/checkEnvironment' import axios from 'axios' -import handleError, { ctx } from './handleError' -import { userAgent } from './helpers' +import { ctx, handleError, userAgent } from './helpers' export type Params = { method: 'get' | 'post' | 'put' | 'delete' @@ -57,14 +56,12 @@ const apiTooot = async ({ }) }) .catch(error => { - Sentry.setExtras({ - API: 'tooot', - request: { url, params, body }, - ...(error?.response && { response: error.response }) - }) - Sentry.captureMessage('API error', { - contexts: { errorObject: error } + Sentry.setContext('API request', { url, params, body }) + Sentry.setContext('Error response', { + ...(error?.response && { response: error.response?._response }) }) + Sentry.setContext('Error object', { error }) + Sentry.captureMessage('API error') return handleError(error) }) diff --git a/src/screens/Compose.tsx b/src/screens/Compose.tsx index 594c5d12..9928c062 100644 --- a/src/screens/Compose.tsx +++ b/src/screens/Compose.tsx @@ -319,9 +319,8 @@ const ScreenCompose: React.FC> = ({ ] ) } else { - Sentry.captureMessage('Compose posting', { - contexts: { errorObject: error } - }) + Sentry.setContext('Error object', { error }) + Sentry.captureMessage('Posting error') haptics('Error') composeDispatch({ type: 'posting', payload: false }) Alert.alert(t('heading.right.alert.default.title'), undefined, [ diff --git a/src/utils/push/useConnect.ts b/src/utils/push/useConnect.ts index a6fc3b09..a34df781 100644 --- a/src/utils/push/useConnect.ts +++ b/src/utils/push/useConnect.ts @@ -33,14 +33,11 @@ const pushUseConnect = () => { }) .then(() => Notifications.setBadgeCountAsync(0)) .catch(error => { - Sentry.setExtras({ - API: 'tooot', - expoToken, - ...(error?.response && { response: error.response }) - }) - Sentry.captureMessage('Push connect error', { - contexts: { errorObject: error } + Sentry.setContext('Error response', { + ...(error?.response && { response: error.response?._response }) }) + Sentry.setContext('Error object', { error }) + Sentry.captureMessage('Push connect error') Notifications.setBadgeCountAsync(0) if (error?.status == 404) { displayMessage({ @@ -84,10 +81,7 @@ const pushUseConnect = () => { } useEffect(() => { - Sentry.setExtras({ - expoToken, - pushEnabledCount: pushEnabled - }) + Sentry.setContext('Push', { expoToken, pushEnabledCount: pushEnabled.length }) if (expoToken && pushEnabled.length) { connect() diff --git a/src/utils/slices/instances/push/register.ts b/src/utils/slices/instances/push/register.ts index 7fc592ea..dd82032c 100644 --- a/src/utils/slices/instances/push/register.ts +++ b/src/utils/slices/instances/push/register.ts @@ -74,8 +74,7 @@ const pushRegister = async ( }) if (!res.body.server_key?.length) { - Sentry.setExtras({ - API: 'tooot', + Sentry.setContext('Push server key', { instance: instanceUri, resBody: res.body })