fix(error): add sanity checks before error destructuring
also preserve original stack trace(and print it) in dev mode
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import * as cheerio from 'cheerio';
|
||||
import RawFind from 'src/interfaces/misc/rawFind';
|
||||
import axiosInstance from 'src/utils/axiosInstance';
|
||||
import axiosInstance, { isSaneError } from 'src/utils/axiosInstance';
|
||||
import { AppError } from 'src/utils/helpers';
|
||||
import cleanFind from 'src/utils/cleaners/find';
|
||||
|
||||
@ -14,11 +14,10 @@ const basicSearch = async (queryStr: string) => {
|
||||
const cleanData = cleanFind(parsedRawData);
|
||||
|
||||
return cleanData;
|
||||
} catch (err: any) {
|
||||
if (err.response?.status === 404)
|
||||
throw new AppError('not found', 404, err.cause);
|
||||
} catch (err) {
|
||||
if (isSaneError(err) && err.response?.status === 404) throw new AppError('not found', 404, err);
|
||||
|
||||
throw new AppError('something went wrong', 500, err.cause);
|
||||
throw new AppError('something went wrong', 500, err);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import * as cheerio from 'cheerio';
|
||||
import RawName from 'src/interfaces/misc/rawName';
|
||||
import axiosInstance from 'src/utils/axiosInstance';
|
||||
import axiosInstance, { isSaneError } from 'src/utils/axiosInstance';
|
||||
import cleanName from 'src/utils/cleaners/name';
|
||||
import { AppError } from 'src/utils/helpers';
|
||||
|
||||
@ -15,10 +15,10 @@ const name = async (nameId: string) => {
|
||||
const cleanData = cleanName(parsedRawData);
|
||||
// returning
|
||||
return cleanData;
|
||||
} catch (err: any) {
|
||||
if (err.response?.status === 404) throw new AppError('not found', 404, err.cause);
|
||||
} catch (err) {
|
||||
if (isSaneError(err) && err.response?.status === 404) throw new AppError('not found', 404, err);
|
||||
|
||||
throw new AppError('something went wrong', 500, err.cause);
|
||||
throw new AppError('something went wrong', 500, err);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import * as cheerio from 'cheerio';
|
||||
import RawTitle from 'src/interfaces/misc/rawTitle';
|
||||
import axiosInstance from 'src/utils/axiosInstance';
|
||||
import axiosInstance, { isSaneError } from 'src/utils/axiosInstance';
|
||||
import cleanTitle from 'src/utils/cleaners/title';
|
||||
import { AppError } from 'src/utils/helpers';
|
||||
|
||||
@ -15,11 +15,10 @@ const title = async (titleId: string) => {
|
||||
const cleanData = cleanTitle(parsedRawData);
|
||||
// returning
|
||||
return cleanData;
|
||||
} catch (err: any) {
|
||||
if (err.response?.status === 404)
|
||||
throw new AppError('not found', 404, err.cause);
|
||||
} catch (err) {
|
||||
if (isSaneError(err) && err.response?.status === 404) throw new AppError('not found', 404, err);
|
||||
|
||||
throw new AppError('something went wrong', 500, err.cause);
|
||||
throw new AppError('something went wrong', 500, err);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user