Implement package limit notification system
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { authorizedFetch } from './auth/tokens';
|
||||
import { ApiError } from './lib/apiError';
|
||||
import i18n from './i18n';
|
||||
|
||||
type JsonValue = Record<string, unknown>;
|
||||
@@ -331,8 +332,20 @@ type EventSavePayload = {
|
||||
async function jsonOrThrow<T>(response: Response, message: string): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const body = await safeJson(response);
|
||||
console.error('[API]', message, response.status, body);
|
||||
throw new Error(message);
|
||||
const status = response.status;
|
||||
const errorPayload = body && typeof body === 'object' ? (body as Record<string, unknown>).error : null;
|
||||
const errorMessage = (errorPayload && typeof errorPayload === 'object' && 'message' in errorPayload && typeof errorPayload.message === 'string')
|
||||
? errorPayload.message
|
||||
: message;
|
||||
const errorCode = errorPayload && typeof errorPayload === 'object' && typeof errorPayload.code === 'string'
|
||||
? errorPayload.code
|
||||
: undefined;
|
||||
const errorMeta = errorPayload && typeof errorPayload === 'object' && typeof errorPayload.meta === 'object'
|
||||
? errorPayload.meta as Record<string, unknown>
|
||||
: undefined;
|
||||
|
||||
console.error('[API]', errorMessage, status, body);
|
||||
throw new ApiError(errorMessage, status, errorCode, errorMeta);
|
||||
}
|
||||
|
||||
return (await response.json()) as T;
|
||||
|
||||
Reference in New Issue
Block a user