use isAxiosError to improve type detection

This commit is contained in:
Hazelnoot 2025-05-06 21:39:45 -04:00
parent fefe2f6db8
commit 38b1e1225c
3 changed files with 17 additions and 25 deletions

View file

@ -97,6 +97,7 @@
"ajv": "8.17.1",
"archiver": "7.0.1",
"argon2": "^0.40.1",
"axios": "1.7.4",
"async-mutex": "0.5.0",
"bcryptjs": "2.4.3",
"blurhash": "2.0.5",

View file

@ -4,6 +4,7 @@
*/
import { Injectable } from '@nestjs/common';
import { isAxiosError } from 'axios';
import type Logger from '@/logger.js';
import { LoggerService } from '@/core/LoggerService.js';
import { ApiError } from '@/server/api/error.js';
@ -59,14 +60,14 @@ export function getErrorException(error: unknown): Error | null {
}
// AxiosErrors need special decoding
if (error.name === 'AxiosError') {
if (isAxiosError(error)) {
// Axios errors with a response are from the remote
if ('response' in error && error.response && typeof (error.response) === 'object') {
if (error.response) {
return null;
}
// This is the inner exception, basically
if ('cause' in error && error.cause instanceof Error) {
if (error.cause && !isAxiosError(error.cause)) {
return error.cause;
}
@ -131,9 +132,9 @@ export function getErrorData(error: unknown): MastodonError {
}
function unpackAxiosError(error: unknown): unknown {
if (error && typeof(error) === 'object') {
if ('response' in error && error.response && typeof (error.response) === 'object') {
if ('data' in error.response && error.response.data && typeof (error.response.data) === 'object') {
if (isAxiosError(error)) {
if (error.response) {
if (error.response.data) {
if ('error' in error.response.data && error.response.data.error && typeof(error.response.data.error) === 'object') {
return error.response.data.error;
}
@ -145,14 +146,12 @@ function unpackAxiosError(error: unknown): unknown {
return undefined;
}
if (error instanceof Error && error.name === 'AxiosError') {
if ('cause' in error) {
return error.cause;
}
// No data - this is a fallback to avoid leaking request/response details in the error
return String(error);
if (error.cause && !isAxiosError(error.cause)) {
return error.cause;
}
// No data - this is a fallback to avoid leaking request/response details in the error
return String(error);
}
return error;

16
pnpm-lock.yaml generated
View file

@ -182,6 +182,9 @@ importers:
async-mutex:
specifier: 0.5.0
version: 0.5.0
axios:
specifier: 1.7.4
version: 1.7.4
bcryptjs:
specifier: 2.4.3
version: 2.4.3
@ -6417,15 +6420,6 @@ packages:
resolution: {integrity: sha512-Be3narBNt2s6bsaqP6Jzq91heDgOEaDCJAXcE3qcma/EJBSy5FB4cvO31XBInuAuKBx8Kptf8dkhjK0IOru39Q==}
engines: {node: '>=18'}
follow-redirects@1.15.2:
resolution: {integrity: sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==}
engines: {node: '>=4.0'}
peerDependencies:
debug: '*'
peerDependenciesMeta:
debug:
optional: true
follow-redirects@1.15.9:
resolution: {integrity: sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==}
engines: {node: '>=4.0'}
@ -15329,7 +15323,7 @@ snapshots:
axios@0.24.0:
dependencies:
follow-redirects: 1.15.2
follow-redirects: 1.15.9(debug@4.4.0)
transitivePeerDependencies:
- debug
@ -17251,8 +17245,6 @@ snapshots:
async: 0.2.10
which: 1.3.1
follow-redirects@1.15.2: {}
follow-redirects@1.15.9(debug@4.4.0):
optionalDependencies:
debug: 4.4.0(supports-color@8.1.1)