cal.pub0.org/apps/api/lib/helpers/safeParseJSON.ts

15 lines
354 B
TypeScript
Raw Normal View History

2022-10-07 07:13:47 +00:00
export default function parseJSONSafely(str: string) {
try {
return JSON.parse(str);
} catch (e) {
console.error((e as Error).message);
if ((e as Error).message.includes("Unexpected token")) {
return {
success: false,
message: `Invalid JSON in the body: ${(e as Error).message}`,
};
}
2022-10-07 07:13:47 +00:00
return {};
}
}