Skip to content

Commit

Permalink
fix: improve 404 api errors (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
belopash committed Mar 12, 2024
1 parent 72eda60 commit 53800f5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/api/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function listSquids({ squidName }: { squidName?: string }) {
method: 'get',
path: `/user/squids`,
query: {
squidName,
name: squidName,
},
});

Expand Down
26 changes: 17 additions & 9 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export abstract class CliCommand extends Command {
}

async catch(error: any) {
const { request, body } = error;

if (error instanceof ApiError) {
const { request, body } = error;

switch (request.status) {
case 401:
return this.error(
Expand All @@ -47,12 +47,20 @@ export abstract class CliCommand extends Command {
}
return this.error(body?.error || body?.message || `Validation error ${body}`);
case 404:
const url = `${chalk.bold(request.method)} ${chalk.bold(request.url)}`;

return this.error(
`Unknown API endpoint ${url}. Check that your are using the latest version of the Squid CLI. If the problem persists, please contact support.`,
);

const defaultErrorStart = `cannot ${request.method.toLowerCase()}`;

if (
body.error.toLowerCase().startsWith(defaultErrorStart) ||
body.message?.toLowerCase().startsWith(defaultErrorStart)
) {
const url = `${chalk.bold(request.method)} ${chalk.bold(request.url)}`;

return this.error(
`Unknown API endpoint ${url}. Check that your are using the latest version of the Squid CLI. If the problem persists, please contact support.`,
);
} else {
return this.error(body.error);
}
case 405:
return this.error(body?.error || body?.message || 'Method not allowed');
case 502:
Expand Down Expand Up @@ -93,7 +101,7 @@ export abstract class CliCommand extends Command {
const squids = await listSquids({ squidName });
const organizations = squids.map((s) => s.organization).filter((o): o is SquidOrganizationResponse => !isNil(o));
if (organizations.length === 0) {
return this.error(`No organizations has been found`);
return this.error(`Squid "${squidName}" was not found.`);
} else if (organizations.length === 1) {
return organizations[0].code;
}
Expand Down

0 comments on commit 53800f5

Please sign in to comment.