fix(error): add sanity checks before error destructuring

also preserve original stack trace(and print it) in dev mode
This commit is contained in:
zyachel
2024-08-23 03:16:05 +05:30
parent 333d3b107e
commit e320557add
16 changed files with 79 additions and 58 deletions

View File

@ -1,6 +1,6 @@
import { AxiosError } from 'axios';
import * as cheerio from 'cheerio';
import axiosInstance from 'src/utils/axiosInstance';
import axiosInstance, { isSaneError } from 'src/utils/axiosInstance';
import { AppError } from 'src/utils/helpers';
const reviews = async (titleId: string, queryStr = '') => {
@ -27,12 +27,12 @@ const reviews = async (titleId: string, queryStr = '') => {
return { meta, list, cursor };
} catch (err) {
if (err instanceof AxiosError && err.response?.status === 404)
throw new AppError('not found', 404, err.cause);
if (isSaneError(err) && err.response?.status === 404)
throw new AppError('not found', 404, err);
if (err instanceof AppError) throw err;
throw new AppError('something went wrong', 500, err instanceof Error ? err.cause : undefined);
throw new AppError('something went wrong', 500, err);
}
};
@ -62,12 +62,12 @@ export const cursoredReviews = async (
return { meta: { title, titleId }, list, cursor };
} catch (err) {
if (err instanceof AxiosError && err.response?.status === 404)
if (isSaneError(err) && err.response?.status === 404)
throw new AppError('not found', 404, err.cause);
if (err instanceof AppError) throw err;
throw new AppError('something went wrong', 500, err instanceof Error ? err.cause : undefined);
throw new AppError('something went wrong', 500, err);
}
};