Skip to content

Commit

Permalink
Change logic for retry, use transient attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Jan 28, 2025
1 parent 983cc9f commit f96baab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 64 deletions.
63 changes: 2 additions & 61 deletions packages/rest-client-utils/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export type ErrorEntity = {
type: 'api_error';
attributes: {
code: string;
transient?: true;
doc_url: string;
details: Record<string, unknown>;
};
};
Expand Down Expand Up @@ -43,46 +45,6 @@ function isErrorBody(body: unknown): body is ErrorBody {
return true;
}

const humanMessageForCode: Record<string, string | undefined> = {
BATCH_DATA_VALIDATION_IN_PROGRESS: `The schema of this model changed, we're re-running validations over every record in background. Please retry with this operation in a few seconds!`,
INSUFFICIENT_PERMISSIONS: 'Your role does not permit this action',
MAINTENANCE_MODE: 'The project is currently in maintenance mode!',
DELETE_RESTRICTION: `Sorry, but you cannot delete this resource, as it's currently used/referenced elsewhere!`,
INVALID_CREDENTIALS: 'Credentials are incorrect!',
INVALID_EMAIL: 'Email address is incorrect!',
INVALID_FORMAT: `The format of the parameters passed is incorrect, take a look at the details of the error to know what's wrong!`,
ITEM_LOCKED:
'The operation cannot be completed as some other user is currently editing this record!',
LINKED_FROM_PUBLISHED_ITEMS: `Couldn't unpublish the record, as some published records are linked to it!`,
PLAN_UPGRADE_REQUIRED: 'Cannot proceed, please upgrade plan!',
PUBLISHED_CHILDREN: `Couldn't unpublish the record, some children records are still published!`,
REQUIRED_2FA_SETUP:
'This project requires every user to turn on 2-factor authentication! Please go to your Dashboard and activate it! (https://dashboard.datocms.com/account/setup-2fa)',
REQUIRED_BY_ASSOCIATION: `Cannot delete the record, as it's required by other records:`,
STALE_ITEM_VERSION:
'Someone else made a change while you were editing this record, please refresh the page!',
TITLE_ALREADY_PRESENT: 'There can only be one Title field per model',
UNPUBLISHED_LINK: `Couldn't publish the record, as it links some unpublished records!`,
UNPUBLISHED_PARENT: `Couldn't publish the record, as the parent record is not published!`,
UPLOAD_IS_CURRENTLY_IN_USE: `Couldn't delete this asset, as it's currently used by some records!`,
UPLOAD_NOT_PASSING_FIELD_VALIDATIONS: `Couldn't update this asset since some records are failing to pass the validations!`,
};

const humanMessageForPlanUpgradeLimit: Record<string, string | undefined> = {
build_triggers: `You've reached the maximum number of build triggers your plan allows`,
sandbox_environments: `You've reached the maximum number of environments your plan allows`,
item_types: `You've reached the maximum number of models your plan allows to create`,
items: `You've reached the maximum number of records your plan allows to create`,
locales: `You've reached the maximum number of locales your plan allows`,
mux_encoding_seconds: `You've reached the maximum video encoding limits of your plan`,
otp: 'Two-factor authentication cannot be on the current plan',
plugins: `You've reached the maximum number of plugins your plan allows`,
roles: `You've reached the maximum number of roles your plan allows to create`,
uploadable_bytes: `You've reached the file storage limits of your plan`,
users: `You've reached the maximum number of collaborators your plan allows to invite to the project`,
access_tokens: `You've reached the maximum number of API tokens your plan allows to create`,
};

export type ApiErrorRequest = {
url: string;
method: string;
Expand Down Expand Up @@ -189,25 +151,4 @@ export class ApiError extends Error {
))),
);
}

get humanMessage() {
const planUpgradeError = this.findError('PLAN_UPGRADE_REQUIRED');

if (planUpgradeError) {
const { limit } = planUpgradeError.attributes.details as {
limit: string;
};
return `${humanMessageForPlanUpgradeLimit[limit]}. Please head over to your account dashboard (https://dashboard.datocms.com/) to upgrade the plan or, if no publicly available plan suits your needs, contact our Sales team (https://www.datocms.com/contact) to get a custom quote!`;
}

const errors = Object.keys(humanMessageForCode)
.filter((code) => this.findError(code))
.map((code) => humanMessageForCode[code]);

if (errors.length === 0) {
return null;
}

return errors.join('\n');
}
}
9 changes: 6 additions & 3 deletions packages/rest-client-utils/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import { buildNormalizedParams } from './buildNormalizedParams';
import {
ApiError,
type ApiErrorInitObject,
TimeoutError,
type ApiErrorInitObject,
type TimeoutErrorInitObject,
} from './errors';
import type { JobResult } from './internalTypes';
Expand Down Expand Up @@ -330,10 +330,13 @@ export async function request<T>(options: RequestOptions): Promise<T> {
),
);

if (autoRetry && error.findError('BATCH_DATA_VALIDATION_IN_PROGRESS')) {
const transientErrorCode = error.errors.find((e) => e.attributes.transient)
?.attributes.code;

if (autoRetry && transientErrorCode) {
if (logLevel >= LogLevel.BASIC) {
log(
`[${requestId}] Data validation in progress, wait ${retryCount} seconds then retry...`,
`[${requestId}] ${transientErrorCode}, wait ${retryCount} seconds then retry...`,
);
}

Expand Down

0 comments on commit f96baab

Please sign in to comment.