Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"author": "Dominik Kundel <dkundel@twilio.com>",
"license": "MIT",
"dependencies": {
"@twilio-labs/serverless-api": "^1.1.0",
"@twilio-labs/serverless-api": "^2.0.1",
"@twilio-labs/serverless-runtime-types": "^1.1.7",
"@types/express": "^4.17.0",
"@types/inquirer": "^6.0.3",
Expand Down
8 changes: 7 additions & 1 deletion src/commands/activate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { printActivateConfig, printActivateResult } from '../printers/activate';
import {
getDebugFunction,
getOraSpinner,
logApiError,
logger,
setLogLevelByName,
} from '../utils/logger';
Expand All @@ -25,7 +26,12 @@ function logError(msg: string) {
function handleError(err: Error, spinner: Ora) {
debug('%O', err);
if (spinner) {
spinner.fail(err.message);
if (err.name === 'TwilioApiError') {
spinner.fail('Failed promoting build.');
logApiError(logger, err);
} else {
spinner.fail(err.message);
}
}
process.exit(1);
}
Expand Down
22 changes: 6 additions & 16 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
DeployLocalProjectConfig,
TwilioServerlessApiClient,
} from '@twilio-labs/serverless-api';
import { ClientApiError } from '@twilio-labs/serverless-api/dist/utils/error';
import { stripIndent } from 'common-tags';
import { Ora } from 'ora';
import path from 'path';
Expand All @@ -10,14 +11,11 @@ import { checkConfigForCredentials } from '../checks/check-credentials';
import checkProjectStructure from '../checks/project-structure';
import { DeployCliFlags, getConfigFromFlags } from '../config/deploy';
import { printConfigInfo, printDeployedResources } from '../printers/deploy';
import {
ApiErrorResponse,
HttpError,
saveLatestDeploymentData,
} from '../serverless-api/utils';
import { HttpError, saveLatestDeploymentData } from '../serverless-api/utils';
import {
getDebugFunction,
getOraSpinner,
logApiError,
logger,
setLogLevelByName,
} from '../utils/logger';
Expand Down Expand Up @@ -60,17 +58,9 @@ function handleError(
> ${constructCommandName(fullCommand, 'deploy', ['--force'])}
`;
logger.error(messageBody, err.message);
} else if (err.name === 'HTTPError') {
const responseBody = JSON.parse(
(err as HttpError).body
) as ApiErrorResponse;
const messageBody = stripIndent`
${responseBody.message}

More info: ${responseBody.more_info}
`;

logger.error(messageBody);
} else if (err.name === 'TwilioApiError') {
const apiError = err as ClientApiError;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to cast this error as ClientApiError but in the activate, list and logs commands you don't?

logApiError(logger, apiError);
} else {
logger.error(err.message);
}
Expand Down
13 changes: 11 additions & 2 deletions src/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { checkConfigForCredentials } from '../checks/check-credentials';
import checkForValidServiceSid from '../checks/check-service-sid';
import { getConfigFromFlags, ListCliFlags, ListConfig } from '../config/list';
import { printListResult } from '../printers/list';
import { getDebugFunction, logger, setLogLevelByName } from '../utils/logger';
import {
getDebugFunction,
logApiError,
logger,
setLogLevelByName,
} from '../utils/logger';
import { ExternalCliOptions, sharedCliOptions } from './shared';
import { CliInfo } from './types';
import { getFullCommand } from './utils';
Expand All @@ -17,7 +22,11 @@ function logError(msg: string) {

function handleError(err: Error) {
debug('%O', err);
logError(err.message);
if (err.name === 'TwilioApiError') {
logApiError(logger, err);
} else {
logError(err.message);
}
process.exit(1);
}

Expand Down
19 changes: 14 additions & 5 deletions src/commands/logs.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import {
TwilioServerlessApiClient,
LogApiResource,
TwilioServerlessApiClient,
} from '@twilio-labs/serverless-api';
import { LogsCliFlags, LogsConfig, getConfigFromFlags } from '../config/logs';
import { Argv } from 'yargs';
import { checkConfigForCredentials } from '../checks/check-credentials';
import checkForValidServiceSid from '../checks/check-service-sid';
import { printLogs, printLog } from '../printers/logs';
import { getDebugFunction, logger, setLogLevelByName } from '../utils/logger';
import { getConfigFromFlags, LogsCliFlags, LogsConfig } from '../config/logs';
import { printLog, printLogs } from '../printers/logs';
import {
getDebugFunction,
logApiError,
logger,
setLogLevelByName,
} from '../utils/logger';
import { ExternalCliOptions, sharedCliOptions } from './shared';
import { CliInfo } from './types';
import { getFullCommand } from './utils';
Expand All @@ -20,7 +25,11 @@ function logError(msg: string) {

function handleError(err: Error) {
debug('%O', err);
logError(err.message);
if (err.name === 'TwilioApiError') {
logApiError(logger, err);
} else {
logError(err.message);
}
process.exit(1);
}

Expand Down
16 changes: 16 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ClientApiError } from '@twilio-labs/serverless-api/dist/utils/error';
import debug from 'debug';
import ora from 'ora';
import { Writable } from 'stream';
import terminalLink from 'terminal-link';
import { errorMessage, warningMessage } from '../printers/utils';

// an empty stream that immediately drops everything. Like /dev/null
Expand Down Expand Up @@ -100,6 +102,20 @@ export function setLogLevelByName(name: LoggingLevelNames) {
logger.config = { level: LoggingLevel[name] };
}

export function logApiError(logger: ILogger, err: ClientApiError) {
let messageBody = err.message;
const moreInfoLink = err.details?.more_info;
if (typeof moreInfoLink === 'string') {
const linkText = terminalLink(moreInfoLink, moreInfoLink, {
fallback: () => moreInfoLink,
});
messageBody += `\n\nMore info: ${linkText}`;
}
const title = `Failed API Request ${err.code}`;

logger.error(messageBody, title);
}

export function getOraSpinner(options?: string | ora.Options): ora.Ora {
let oraOptions: ora.Options;
if (typeof options === 'string') {
Expand Down