1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00
This commit is contained in:
xmflsct
2022-08-12 16:44:28 +02:00
parent a2721d21c0
commit 397c6a2f44
24 changed files with 599 additions and 937 deletions

View File

@ -1,8 +1,6 @@
import axios from 'axios'
import chalk from 'chalk'
import Constants from 'expo-constants'
const ctx = new chalk.Instance({ level: 3 })
import handleError, { ctx } from './handleError'
export type Params = {
method: 'get' | 'post' | 'put' | 'delete'
@ -57,40 +55,7 @@ const apiGeneral = async <T = unknown>({
body: response.data
})
})
.catch(error => {
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 general '),
ctx.bold('response'),
error.response.status,
error.response.data.error
)
return Promise.reject({
status: error?.response.status,
message: error?.response.data.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 general '),
ctx.bold('request'),
error.request
)
return Promise.reject()
} else {
console.error(
ctx.bold(' API general '),
ctx.bold('internal'),
error?.message,
url
)
return Promise.reject()
}
})
.catch(handleError)
}
export default apiGeneral

38
src/api/handleError.ts Normal file
View File

@ -0,0 +1,38 @@
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 instance '),
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 instance '), ctx.bold('request'), error)
return Promise.reject()
} else {
console.error(
ctx.bold(' API instance '),
ctx.bold('internal'),
error?.message
)
return Promise.reject()
}
}
export default handleError

View File

@ -1,10 +1,8 @@
import { RootState } from '@root/store'
import axios, { AxiosRequestConfig } from 'axios'
import chalk from 'chalk'
import Constants from 'expo-constants'
import li from 'li'
const ctx = new chalk.Instance({ level: 3 })
import handleError, { ctx } from './handleError'
export type Params = {
method: 'get' | 'post' | 'put' | 'delete' | 'patch'
@ -99,36 +97,7 @@ const apiInstance = async <T = unknown>({
links: { prev, next }
})
})
.catch(error => {
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 instance '),
ctx.bold('response'),
error.response.status,
error.response.data.error
)
return Promise.reject({
status: error?.response.status,
message: error?.response.data.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 instance '), ctx.bold('request'), error)
return Promise.reject()
} else {
console.error(
ctx.bold(' API instance '),
ctx.bold('internal'),
error?.message,
url
)
return Promise.reject()
}
})
.catch(handleError)
}
export default apiInstance

View File

@ -1,10 +1,8 @@
import { mapEnvironment } from '@utils/checkEnvironment'
import axios from 'axios'
import chalk from 'chalk'
import Constants from 'expo-constants'
import * as Sentry from 'sentry-expo'
const ctx = new chalk.Instance({ level: 3 })
import handleError, { ctx } from './handleError'
export type Params = {
method: 'get' | 'post' | 'put' | 'delete'
@ -75,38 +73,7 @@ const apiTooot = async <T = unknown>({
Sentry.Native.captureException(error)
}
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 tooot '),
ctx.bold('response'),
error.response.status,
error.response.data.error
)
return Promise.reject({
status: error?.response.status,
message: error?.response.data.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 tooot '),
ctx.bold('request'),
error.request
)
return Promise.reject()
} else {
console.error(
ctx.bold(' API tooot '),
ctx.bold('internal'),
error?.message,
url
)
return Promise.reject()
}
return handleError(error)
})
}