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);
|
2022-10-07 07:42:57 +00:00
|
|
|
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 {};
|
|
|
|
}
|
|
|
|
}
|