Refine logging

This commit is contained in:
xmflsct 2022-12-04 14:39:27 +01:00
parent 8d0a58b975
commit 79246ff821
8 changed files with 50 additions and 64 deletions

View File

@ -1,6 +1,5 @@
import axios from 'axios' import axios from 'axios'
import handleError, { ctx } from './handleError' import { ctx, handleError, userAgent } from './helpers'
import { userAgent } from './helpers'
export type Params = { export type Params = {
method: 'get' | 'post' | 'put' | 'delete' method: 'get' | 'post' | 'put' | 'delete'

View File

@ -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

View File

@ -1,6 +1,36 @@
import Constants from "expo-constants" import chalk from 'chalk'
import { Platform } from "react-native" 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 } 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 }

View File

@ -1,8 +1,7 @@
import { RootState } from '@root/store' import { RootState } from '@root/store'
import axios, { AxiosRequestConfig } from 'axios' import axios, { AxiosRequestConfig } from 'axios'
import li from 'li' import li from 'li'
import handleError, { ctx } from './handleError' import { ctx, handleError, userAgent } from './helpers'
import { userAgent } from './helpers'
export type Params = { export type Params = {
method: 'get' | 'post' | 'put' | 'delete' | 'patch' method: 'get' | 'post' | 'put' | 'delete' | 'patch'

View File

@ -1,8 +1,7 @@
import * as Sentry from '@sentry/react-native' import * as Sentry from '@sentry/react-native'
import { mapEnvironment } from '@utils/checkEnvironment' import { mapEnvironment } from '@utils/checkEnvironment'
import axios from 'axios' import axios from 'axios'
import handleError, { ctx } from './handleError' import { ctx, handleError, userAgent } from './helpers'
import { userAgent } from './helpers'
export type Params = { export type Params = {
method: 'get' | 'post' | 'put' | 'delete' method: 'get' | 'post' | 'put' | 'delete'
@ -57,14 +56,12 @@ const apiTooot = async <T = unknown>({
}) })
}) })
.catch(error => { .catch(error => {
Sentry.setExtras({ Sentry.setContext('API request', { url, params, body })
API: 'tooot', Sentry.setContext('Error response', {
request: { url, params, body }, ...(error?.response && { response: error.response?._response })
...(error?.response && { response: error.response })
})
Sentry.captureMessage('API error', {
contexts: { errorObject: error }
}) })
Sentry.setContext('Error object', { error })
Sentry.captureMessage('API error')
return handleError(error) return handleError(error)
}) })

View File

@ -319,9 +319,8 @@ const ScreenCompose: React.FC<RootStackScreenProps<'Screen-Compose'>> = ({
] ]
) )
} else { } else {
Sentry.captureMessage('Compose posting', { Sentry.setContext('Error object', { error })
contexts: { errorObject: error } Sentry.captureMessage('Posting error')
})
haptics('Error') haptics('Error')
composeDispatch({ type: 'posting', payload: false }) composeDispatch({ type: 'posting', payload: false })
Alert.alert(t('heading.right.alert.default.title'), undefined, [ Alert.alert(t('heading.right.alert.default.title'), undefined, [

View File

@ -33,14 +33,11 @@ const pushUseConnect = () => {
}) })
.then(() => Notifications.setBadgeCountAsync(0)) .then(() => Notifications.setBadgeCountAsync(0))
.catch(error => { .catch(error => {
Sentry.setExtras({ Sentry.setContext('Error response', {
API: 'tooot', ...(error?.response && { response: error.response?._response })
expoToken,
...(error?.response && { response: error.response })
})
Sentry.captureMessage('Push connect error', {
contexts: { errorObject: error }
}) })
Sentry.setContext('Error object', { error })
Sentry.captureMessage('Push connect error')
Notifications.setBadgeCountAsync(0) Notifications.setBadgeCountAsync(0)
if (error?.status == 404) { if (error?.status == 404) {
displayMessage({ displayMessage({
@ -84,10 +81,7 @@ const pushUseConnect = () => {
} }
useEffect(() => { useEffect(() => {
Sentry.setExtras({ Sentry.setContext('Push', { expoToken, pushEnabledCount: pushEnabled.length })
expoToken,
pushEnabledCount: pushEnabled
})
if (expoToken && pushEnabled.length) { if (expoToken && pushEnabled.length) {
connect() connect()

View File

@ -74,8 +74,7 @@ const pushRegister = async (
}) })
if (!res.body.server_key?.length) { if (!res.body.server_key?.length) {
Sentry.setExtras({ Sentry.setContext('Push server key', {
API: 'tooot',
instance: instanceUri, instance: instanceUri,
resBody: res.body resBody: res.body
}) })