@vitrical/utils
Advanced tools
Comparing version 1.5.2 to 1.5.3
@@ -1,2 +0,1 @@ | ||
import { AxiosError } from 'axios'; | ||
export declare class ApiError extends Error { | ||
@@ -11,3 +10,3 @@ url: string; | ||
} | ||
export declare const handleApiError: (url: string, newStack: string, err: AxiosError) => ApiError | Error | unknown; | ||
export declare const handleApiError: (url: string, newStack: string, err: unknown) => ApiError | Error | unknown; | ||
export type CustomRequestResponse<R = any> = { | ||
@@ -14,0 +13,0 @@ status: number; |
53
api.js
@@ -18,12 +18,22 @@ "use strict"; | ||
const handleApiError = (url, newStack, err) => { | ||
if (!err?.response) { | ||
if (typeof err === 'string') { | ||
const newError = new Error(err); | ||
newError.stack = newStack; | ||
newError.message = err; | ||
return newError; | ||
} | ||
if (typeof err === 'string') { | ||
const newError = new Error(err); | ||
newError.stack = newStack; | ||
return newError; | ||
} | ||
if (typeof err !== 'object' || !err) { | ||
return err; | ||
} | ||
if (err?.response?.status && typeof err?.response?.status === 'number' && !!err?.response?.data) { | ||
if (!('response' in err)) { | ||
return err; | ||
} | ||
if (typeof err.response === 'string') { | ||
const newError = new Error(err.response); | ||
newError.stack = newStack; | ||
return newError; | ||
} | ||
if (typeof err.response !== 'object' || !err.response) { | ||
return err; | ||
} | ||
if ('status' in err.response && typeof err?.response?.status === 'number') { | ||
if (typeof err === 'object' && | ||
@@ -38,3 +48,3 @@ !!err && | ||
} | ||
const body = err.response.data; | ||
const body = 'data' in err.response ? err.response.data : err.response; | ||
let requestMsg = undefined; | ||
@@ -45,15 +55,21 @@ if (typeof body === 'string') { | ||
else if (typeof body === 'object' && !!body) { | ||
if ('msg' in body && typeof body?.msg === 'string') { | ||
if ('msg' in body && typeof body.msg === 'string') { | ||
requestMsg = body.msg; | ||
} | ||
else if ('error' in body && typeof body?.error === 'string') { | ||
else if ('error' in body && typeof body.error === 'string') { | ||
requestMsg = body.error; | ||
} | ||
else if ('message' in body && typeof body?.message === 'string') { | ||
else if ('message' in body && typeof body.message === 'string') { | ||
requestMsg = body.message; | ||
} | ||
else if ('text' in body && typeof body?.text === 'string') { | ||
else if ('text' in body && typeof body.text === 'string') { | ||
requestMsg = body.text; | ||
} | ||
else { | ||
requestMsg = JSON.stringify(body); | ||
} | ||
} | ||
else { | ||
requestMsg = JSON.stringify(body); | ||
} | ||
const message = requestMsg | ||
@@ -63,4 +79,9 @@ ? `Request failed with status code ${err.response.status} ( ${requestMsg} )` | ||
const apiError = new ApiError(message); | ||
apiError.body = err.response.data; | ||
apiError.headers = err.response.headers; | ||
apiError.body = 'data' in err.response ? err.response.data : err.response; | ||
apiError.headers = | ||
'headers' in err.response && | ||
typeof err.response.headers === 'object' && | ||
!!err.response.headers | ||
? err.response.headers | ||
: {}; | ||
apiError.status = err.response.status; | ||
@@ -73,3 +94,3 @@ apiError.stack = newStack; | ||
else { | ||
if ('message' in err) { | ||
if ('message' in err && typeof err.message === 'string') { | ||
const newError = new Error(err.message); | ||
@@ -76,0 +97,0 @@ newError.stack = newStack; |
{ | ||
"name": "@vitrical/utils", | ||
"version": "1.5.2", | ||
"version": "1.5.3", | ||
"description": "Collection of useful functions and typings", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
48194
723
2