fix(error): add sanity checks before error destructuring
also preserve original stack trace(and print it) in dev mode
This commit is contained in:
@ -3,7 +3,7 @@ import Find, { type FindQueryParams } from 'src/interfaces/shared/search';
|
||||
import basicSearch from 'src/utils/fetchers/basicSearch';
|
||||
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
|
||||
import { findKey } from 'src/utils/constants/keys';
|
||||
import { AppError, cleanQueryStr } from 'src/utils/helpers';
|
||||
import { AppError, cleanQueryStr, getErrorProperties } from 'src/utils/helpers';
|
||||
import { findFilterable } from 'src/utils/constants/find';
|
||||
|
||||
type ResponseData =
|
||||
@ -25,8 +25,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
||||
const results = await getOrSetApiCache(findKey(queryStr), basicSearch, queryStr);
|
||||
|
||||
res.status(200).json({ status: true, data: { title: query, results } });
|
||||
} catch (error: any) {
|
||||
const { message = 'Not found', statusCode = 404 } = error;
|
||||
} catch (error) {
|
||||
const { message, statusCode } = getErrorProperties(error);
|
||||
res.status(statusCode).json({ status: false, message });
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import type Name from 'src/interfaces/shared/name';
|
||||
import name from 'src/utils/fetchers/name';
|
||||
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
|
||||
import { nameKey } from 'src/utils/constants/keys';
|
||||
import { AppError } from 'src/utils/helpers';
|
||||
import { AppError, getErrorProperties } from 'src/utils/helpers';
|
||||
|
||||
type ResponseData = { status: true; data: Name } | { status: false; message: string };
|
||||
|
||||
@ -15,8 +15,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
||||
|
||||
const data = await getOrSetApiCache(nameKey(nameId), name, nameId);
|
||||
res.status(200).json({ status: true, data });
|
||||
} catch (error: any) {
|
||||
const { message = 'Not found', statusCode = 404 } = error;
|
||||
} catch (error) {
|
||||
const { message, statusCode } = getErrorProperties(error);
|
||||
|
||||
res.status(statusCode).json({ status: false, message });
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import type Title from 'src/interfaces/shared/title';
|
||||
import title from 'src/utils/fetchers/title';
|
||||
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
|
||||
import { titleKey } from 'src/utils/constants/keys';
|
||||
import { AppError } from 'src/utils/helpers';
|
||||
import { AppError, getErrorProperties } from 'src/utils/helpers';
|
||||
|
||||
type ResponseData = { status: true; data: Title } | { status: false; message: string };
|
||||
|
||||
@ -14,8 +14,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
||||
const titleId = req.query.titleId as string;
|
||||
const data = await getOrSetApiCache(titleKey(titleId), title, titleId);
|
||||
res.status(200).json({ status: true, data });
|
||||
} catch (error: any) {
|
||||
const { message = 'Not found', statusCode = 404 } = error;
|
||||
} catch (error) {
|
||||
const { message, statusCode } = getErrorProperties(error);
|
||||
res.status(statusCode).json({ status: false, message });
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import type { TitleReviewsCursored } from 'src/interfaces/shared/titleReviews';
|
||||
import { cursoredReviews } from 'src/utils/fetchers/titleReviews';
|
||||
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
|
||||
import { titleReviewsKey } from 'src/utils/constants/keys';
|
||||
import { AppError, cleanQueryStr } from 'src/utils/helpers';
|
||||
import { AppError, cleanQueryStr, getErrorProperties } from 'src/utils/helpers';
|
||||
import { keys as titleReviewsQueryKeys } from 'src/utils/constants/titleReviewsFilters';
|
||||
|
||||
type ResponseData =
|
||||
@ -26,8 +26,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
||||
queryStr
|
||||
);
|
||||
res.status(200).json({ status: true, data });
|
||||
} catch (error: any) {
|
||||
const { message = 'Not found', statusCode = 404 } = error;
|
||||
} catch (error) {
|
||||
const { message, statusCode } = getErrorProperties(error);
|
||||
res.status(statusCode).json({ status: false, message });
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import reviews from 'src/utils/fetchers/titleReviews';
|
||||
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
|
||||
import { titleReviewsKey } from 'src/utils/constants/keys';
|
||||
import { keys } from 'src/utils/constants/titleReviewsFilters';
|
||||
import { AppError, cleanQueryStr } from 'src/utils/helpers';
|
||||
import { AppError, cleanQueryStr, getErrorProperties } from 'src/utils/helpers';
|
||||
|
||||
type ResponseData = { status: true; data: TitleReviews } | { status: false; message: string };
|
||||
|
||||
@ -22,8 +22,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
||||
queryStr
|
||||
);
|
||||
res.status(200).json({ status: true, data });
|
||||
} catch (error: any) {
|
||||
const { message = 'Not found', statusCode = 404 } = error;
|
||||
} catch (error) {
|
||||
const { message, statusCode } = getErrorProperties(error);
|
||||
res.status(statusCode).json({ status: false, message });
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import Find, { FindQueryParams } from 'src/interfaces/shared/search';
|
||||
import { AppError } from 'src/interfaces/shared/error';
|
||||
import basicSearch from 'src/utils/fetchers/basicSearch';
|
||||
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
|
||||
import { cleanQueryStr } from 'src/utils/helpers';
|
||||
import { cleanQueryStr, getErrorProperties } from 'src/utils/helpers';
|
||||
import { findKey } from 'src/utils/constants/keys';
|
||||
import styles from 'src/styles/modules/pages/find/find.module.scss';
|
||||
import { findFilterable } from 'src/utils/constants/find';
|
||||
@ -66,8 +66,8 @@ export const getServerSideProps: GetServerSideProps<Data, FindQueryParams> = asy
|
||||
return {
|
||||
props: { data: { title: query, results: res }, error: null, originalPath },
|
||||
};
|
||||
} catch (error: any) {
|
||||
const { message, statusCode } = error;
|
||||
} catch (error) {
|
||||
const { message, statusCode } = getErrorProperties(error);
|
||||
ctx.res.statusCode = statusCode;
|
||||
ctx.res.statusMessage = message;
|
||||
|
||||
|
@ -8,7 +8,7 @@ import Name from 'src/interfaces/shared/name';
|
||||
import { AppError } from 'src/interfaces/shared/error';
|
||||
import name from 'src/utils/fetchers/name';
|
||||
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
|
||||
import { getProxiedIMDbImgUrl } from 'src/utils/helpers';
|
||||
import { getErrorProperties, getProxiedIMDbImgUrl } from 'src/utils/helpers';
|
||||
import { nameKey } from 'src/utils/constants/keys';
|
||||
import styles from 'src/styles/modules/pages/name/name.module.scss';
|
||||
|
||||
@ -54,9 +54,8 @@ export const getServerSideProps: GetServerSideProps<Data, Params> = async ctx =>
|
||||
const data = await getOrSetApiCache(nameKey(nameId), name, nameId);
|
||||
|
||||
return { props: { data, error: null, originalPath } };
|
||||
} catch (error: any) {
|
||||
const { message, statusCode } = error;
|
||||
|
||||
} catch (error) {
|
||||
const { message, statusCode } = getErrorProperties(error);
|
||||
ctx.res.statusCode = statusCode;
|
||||
ctx.res.statusMessage = message;
|
||||
|
||||
|
@ -8,7 +8,7 @@ import Title from 'src/interfaces/shared/title';
|
||||
import { AppError } from 'src/interfaces/shared/error';
|
||||
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
|
||||
import title from 'src/utils/fetchers/title';
|
||||
import { getProxiedIMDbImgUrl } from 'src/utils/helpers';
|
||||
import { getErrorProperties, getProxiedIMDbImgUrl } from 'src/utils/helpers';
|
||||
import { titleKey } from 'src/utils/constants/keys';
|
||||
import styles from 'src/styles/modules/pages/title/title.module.scss';
|
||||
|
||||
@ -63,8 +63,8 @@ export const getServerSideProps: GetServerSideProps<Data, Params> = async ctx =>
|
||||
const data = await getOrSetApiCache(titleKey(titleId), title, titleId);
|
||||
|
||||
return { props: { data, error: null, originalPath } };
|
||||
} catch (error: any) {
|
||||
const { message, statusCode } = error;
|
||||
} catch (error) {
|
||||
const { message, statusCode } = getErrorProperties(error);
|
||||
ctx.res.statusCode = statusCode;
|
||||
ctx.res.statusMessage = message;
|
||||
|
||||
|
@ -6,7 +6,7 @@ import { BasicCard, Filters, Pagination, Reviews } from 'src/components/titleRev
|
||||
import { AppError } from 'src/interfaces/shared/error';
|
||||
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
|
||||
import { cursoredReviews } from 'src/utils/fetchers/titleReviews';
|
||||
import { cleanQueryStr } from 'src/utils/helpers';
|
||||
import { cleanQueryStr, getErrorProperties } from 'src/utils/helpers';
|
||||
import { titleReviewsKey } from 'src/utils/constants/keys';
|
||||
import styles from 'src/styles/modules/pages/titleReviews/titleReviews.module.scss';
|
||||
import { TitleReviewsCursored } from 'src/interfaces/shared/titleReviews';
|
||||
@ -60,8 +60,8 @@ export const getServerSideProps: GetServerSideProps<Data, Params> = async ctx =>
|
||||
);
|
||||
|
||||
return { props: { data, error: null, originalPath } };
|
||||
} catch (error: any) {
|
||||
const { message, statusCode } = error;
|
||||
} catch (error) {
|
||||
const { message, statusCode } = getErrorProperties(error);
|
||||
ctx.res.statusCode = statusCode;
|
||||
ctx.res.statusMessage = message;
|
||||
|
||||
|
@ -6,7 +6,7 @@ import { Filters, Pagination, Reviews, TitleCard } from 'src/components/titleRev
|
||||
import { AppError } from 'src/interfaces/shared/error';
|
||||
import getOrSetApiCache from 'src/utils/getOrSetApiCache';
|
||||
import titleReviews from 'src/utils/fetchers/titleReviews';
|
||||
import { cleanQueryStr, getProxiedIMDbImgUrl } from 'src/utils/helpers';
|
||||
import { cleanQueryStr, getErrorProperties, getProxiedIMDbImgUrl } from 'src/utils/helpers';
|
||||
import { titleReviewsKey } from 'src/utils/constants/keys';
|
||||
import styles from 'src/styles/modules/pages/titleReviews/titleReviews.module.scss';
|
||||
import TitleReviews from 'src/interfaces/shared/titleReviews';
|
||||
@ -80,8 +80,8 @@ export const getServerSideProps: GetServerSideProps<Data, Params> = async ctx =>
|
||||
);
|
||||
|
||||
return { props: { data, error: null, originalPath } };
|
||||
} catch (error: any) {
|
||||
const { message, statusCode } = error;
|
||||
} catch (error) {
|
||||
const { message, statusCode } = getErrorProperties(error);
|
||||
ctx.res.statusCode = statusCode;
|
||||
ctx.res.statusMessage = message;
|
||||
|
||||
|
Reference in New Issue
Block a user