Skip to content

Commit

Permalink
fix(error): add sanity checks before error destructuring
Browse files Browse the repository at this point in the history
also preserve original stack trace(and print it) in dev mode
  • Loading branch information
zyachel committed Aug 24, 2024
1 parent 333d3b1 commit e320557
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 58 deletions.
6 changes: 3 additions & 3 deletions src/pages/api/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 });
}
}
7 changes: 4 additions & 3 deletions src/pages/api/name/[nameId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand All @@ -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 });
}
}
6 changes: 3 additions & 3 deletions src/pages/api/title/[titleId]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand All @@ -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 });
}
}
6 changes: 3 additions & 3 deletions src/pages/api/title/[titleId]/reviews/[paginationKey].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 });
}
}
6 changes: 3 additions & 3 deletions src/pages/api/title/[titleId]/reviews/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand All @@ -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 });
}
}
6 changes: 3 additions & 3 deletions src/pages/find/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 3 additions & 4 deletions src/pages/name/[nameId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/pages/title/[titleId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/pages/title/[titleId]/reviews/[paginationKey]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/pages/title/[titleId]/reviews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions src/utils/axiosInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ const axiosInstance = axios.create({
},
});


export const isSaneError = axios.isAxiosError;

export default axiosInstance;
9 changes: 4 additions & 5 deletions src/utils/fetchers/basicSearch.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
}
};

Expand Down
8 changes: 4 additions & 4 deletions src/utils/fetchers/name.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
}
};

Expand Down
9 changes: 4 additions & 5 deletions src/utils/fetchers/title.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
}
};

Expand Down
12 changes: 6 additions & 6 deletions src/utils/fetchers/titleReviews.ts
Original file line number Diff line number Diff line change
@@ -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 = '') => {
Expand All @@ -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);
}
};

Expand Down Expand Up @@ -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);
}
};

Expand Down
34 changes: 27 additions & 7 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ export const getProxiedIMDbImgUrl = (url: string) => {
};

export const AppError = class extends Error {
constructor(message: string, public statusCode: number, cause?: any) {
super(message, cause);
constructor(message: string, public statusCode: number, errorOptions?: unknown) {
const saneErrorOptions = getErrorOptions(errorOptions);
super(message, saneErrorOptions);

Error.captureStackTrace(this, AppError);
if (process.env.NODE_ENV === 'development') console.error(this);
}
};

Expand All @@ -90,10 +92,7 @@ export const cleanQueryStr = (record: Record<string, string>, filterable: string
return urlSearchParams.toString();
};

export const getResTitleTypeHeading = (
type: ResultMetaTypes,
titleType: ResultMetaTitleTypes
) => {
export const getResTitleTypeHeading = (type: ResultMetaTypes, titleType: ResultMetaTitleTypes) => {
if (type !== 'TITLE') return 'Titles';

for (let i = 0; i < resultTitleTypes.types.length; i++) {
Expand All @@ -102,12 +101,33 @@ export const getResTitleTypeHeading = (
}
};


export const isLocalStorageAvailable = () => {
try {
localStorage.getItem('test');
return true;
} catch (e) {
return false;
}
};

const getErrorOptions = (error: unknown): ErrorOptions | undefined => {
if (!error || typeof error !== 'object') return undefined;

let cause: unknown;
// @ts-expect-error it's not an error! just that project's ts version is old, which can't be upgraded
if ('cause' in error) cause = error.cause;
// @ts-expect-error it's not an error! just that project's ts version is old, which can't be upgraded
else if ('stack' in error) cause = error.stack;

// @ts-expect-error it's not an error! just that project's ts version is old, which can't be upgraded
return { cause };
};

export const getErrorProperties = (
error: unknown,
message = 'Something went very wrong',
statusCode = 500
) => {
if (error instanceof AppError) return error;
return new AppError(message, statusCode, error);
};

0 comments on commit e320557

Please sign in to comment.