Files
ontime/apps/server/src/api-data/sheets/googleApi.utils.ts
T
2025-12-18 14:42:01 +01:00

41 lines
1.1 KiB
TypeScript

// https://developers.google.com/calendar/api/guides/errors
interface GoogleApiError {
code: number;
message: string;
errors?: {
message: string;
domain: string;
reason: string;
}[];
}
/**
* Checks whether an error is a Google API error
*/
function isGoogleApiError(error: any): error is GoogleApiError {
return (
typeof error === 'object' &&
error !== null &&
typeof error.code === 'number' &&
Array.isArray(error.errors) &&
typeof error.errors[0]?.reason === 'string' &&
typeof error.errors[0]?.message === 'string'
);
}
/**
* Extract utility to handle a common error where a user imports an xlsx file instead of a Google Sheet
*/
export function catchCommonImportXlsxError(error: any) {
if (
isGoogleApiError(error) &&
error.code === 400 &&
Array.isArray(error.errors) &&
error.errors.length > 0 &&
error.errors[0]?.reason === 'failedPrecondition' &&
error.errors[0]?.message === 'This operation is not supported for this document'
) {
throw new Error('Cannot read the linked file as a Google Sheet. It may be an .xlsx file instead.');
}
}