cal/packages/lib/errors.ts
alannnc 525ce0d075
Fix/office365 api call (#3534)
* WIP fixes for handling office365 api call

* First working version microsoft api call handling

* Remove logs

* Clean up and improve function names

* Clean up

* Remove log

* Fix function missing its correct name

* Fix direct return of statement

* Cleanup

* Simplify with private fetcher

* Update CalendarService.ts

* Update getOfficeAppKeys.ts

Co-authored-by: zomars <zomars@me.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-07-27 19:12:42 +00:00

38 lines
1.1 KiB
TypeScript

export function getErrorFromUnknown(cause: unknown): Error & { statusCode?: number; code?: string } {
if (cause instanceof Error) {
return cause;
}
if (typeof cause === "string") {
// @ts-expect-error https://github.com/tc39/proposal-error-cause
return new Error(cause, { cause });
}
return new Error(`Unhandled error of type '${typeof cause}''`);
}
export function handleErrorsJson(response: Response) {
if (response.headers.get("content-encoding") === "gzip") {
return response.text();
}
if (response.status === 204) {
return new Promise((resolve) => resolve({}));
}
if (!response.ok && response.status < 200 && response.status >= 300) {
response.json().then(console.log);
throw Error(response.statusText);
}
return response.json();
}
export function handleErrorsRaw(response: Response) {
if (response.status === 204) {
return "{}";
}
if (!response.ok && response.status < 200 && response.status >= 300) {
response.text().then(console.log);
throw Error(response.statusText);
}
return response.text();
}