@unito/integration-cli
Version:
Integration CLI
170 lines (169 loc) • 8.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigurationInvalid = exports.EntryDecryptionError = exports.IntegrationAlreadyExistsError = exports.CredentialAccessDenied = exports.CredentialAuthenticationError = exports.UnsupportedContentTypeError = exports.DecryptionAuthenticationError = exports.AuthenticationFailed = exports.MissingApiKey = exports.FileSizeExceeded = exports.MissingAuth2AuthorizationError = exports.InvalidRequestContentTypeError = exports.FailedToRetrieveAccessTokenError = exports.NoRefreshTokenError = exports.ConfigurationMalformed = exports.NoConfigurationFileError = exports.NoIntegrationFoundError = void 0;
exports.handleError = handleError;
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const path_1 = tslib_1.__importDefault(require("path"));
const json_colorizer_1 = tslib_1.__importDefault(require("json-colorizer"));
const configuration_1 = require("./resources/configuration");
const integrationsPlatform_1 = require("./services/integrationsPlatform");
class NoIntegrationFoundError extends Error {
}
exports.NoIntegrationFoundError = NoIntegrationFoundError;
class NoConfigurationFileError extends Error {
}
exports.NoConfigurationFileError = NoConfigurationFileError;
class ConfigurationMalformed extends Error {
}
exports.ConfigurationMalformed = ConfigurationMalformed;
class NoRefreshTokenError extends Error {
}
exports.NoRefreshTokenError = NoRefreshTokenError;
class FailedToRetrieveAccessTokenError extends Error {
}
exports.FailedToRetrieveAccessTokenError = FailedToRetrieveAccessTokenError;
class InvalidRequestContentTypeError extends Error {
}
exports.InvalidRequestContentTypeError = InvalidRequestContentTypeError;
class MissingAuth2AuthorizationError extends Error {
}
exports.MissingAuth2AuthorizationError = MissingAuth2AuthorizationError;
class FileSizeExceeded extends Error {
}
exports.FileSizeExceeded = FileSizeExceeded;
class MissingApiKey extends Error {
}
exports.MissingApiKey = MissingApiKey;
class AuthenticationFailed extends Error {
}
exports.AuthenticationFailed = AuthenticationFailed;
class DecryptionAuthenticationError extends Error {
}
exports.DecryptionAuthenticationError = DecryptionAuthenticationError;
class UnsupportedContentTypeError extends Error {
}
exports.UnsupportedContentTypeError = UnsupportedContentTypeError;
class CredentialAuthenticationError extends Error {
}
exports.CredentialAuthenticationError = CredentialAuthenticationError;
class CredentialAccessDenied extends Error {
}
exports.CredentialAccessDenied = CredentialAccessDenied;
class IntegrationAlreadyExistsError extends Error {
}
exports.IntegrationAlreadyExistsError = IntegrationAlreadyExistsError;
class EntryDecryptionError extends Error {
key;
environment;
constructor(key, targetEnvironment) {
super();
this.key = key;
this.environment = targetEnvironment;
}
}
exports.EntryDecryptionError = EntryDecryptionError;
class ConfigurationInvalid extends Error {
details;
prettyDetails;
constructor(message, details, prettyDetails) {
super(message);
this.details = details;
this.prettyDetails = prettyDetails;
}
}
exports.ConfigurationInvalid = ConfigurationInvalid;
function handleError(command, error) {
/* istanbul ignore if */
if (core_1.ux.action.running) {
core_1.ux.action.stop(chalk_1.default.redBright('failed'));
}
let unhandled = false;
if (error instanceof NoIntegrationFoundError) {
command.logToStderr(chalk_1.default.redBright('Your directory does not seem to contain an integration :('));
command.logToStderr(`Make sure you are in the right directory and that you have a ${chalk_1.default.yellowBright(configuration_1.DEFAULT_CONFIGURATION_NAME)} file.`);
}
else if (error instanceof NoConfigurationFileError) {
command.logToStderr(chalk_1.default.redBright('This command requires a configuration file for your integration :('));
command.logToStderr(`This file should be located at ${chalk_1.default.yellowBright(path_1.default.relative(process.cwd(), (0, configuration_1.getConfigurationPath)()))}.`);
}
else if (error instanceof ConfigurationMalformed) {
command.logToStderr(chalk_1.default.redBright(`Your ${chalk_1.default.yellowBright(configuration_1.DEFAULT_CONFIGURATION_NAME)} was found but its not a valid JSON file!`));
}
else if (error instanceof ConfigurationInvalid) {
command.logToStderr(chalk_1.default.redBright(`Your ${chalk_1.default.yellowBright(configuration_1.DEFAULT_CONFIGURATION_NAME)} is invalid!`));
command.logToStderr();
command.logToStderr(error.prettyDetails);
}
else if (error instanceof NoRefreshTokenError) {
command.logToStderr(chalk_1.default.redBright('No refresh token found in your configuration file'));
}
else if (error instanceof FailedToRetrieveAccessTokenError) {
command.logToStderr(chalk_1.default.redBright('Failed to retrieve access token'));
command.logToStderr();
command.logToStderr(error.message);
}
else if (error instanceof InvalidRequestContentTypeError) {
command.logToStderr(chalk_1.default.redBright('Invalid request content type'));
command.logToStderr();
command.logToStderr(error.message);
}
else if (error instanceof integrationsPlatform_1.HttpError) {
command.logToStderr();
command.logToStderr((0, json_colorizer_1.default)(JSON.stringify(error.data, null, 2)));
}
else if (error instanceof MissingApiKey) {
command.logToStderr();
command.logToStderr(chalk_1.default.redBright('Your API key is not set!'));
command.logToStderr(`Make sure to run the ${chalk_1.default.yellowBright('login')} command first.`);
}
else if (error instanceof AuthenticationFailed) {
command.logToStderr();
command.logToStderr(chalk_1.default.redBright('This command requires that you are logged to Unito!'));
command.logToStderr(`Make sure to run the ${chalk_1.default.yellowBright('login')} command first.`);
}
else if (error instanceof DecryptionAuthenticationError) {
command.logToStderr();
command.logToStderr(chalk_1.default.redBright('A secret cannot be decrypted!'));
command.logToStderr('Your configuration contains secrets (i.e. in top-level secrets, in test accounts, etc.).');
command.logToStderr('To decrypt those secrets locally, you must authenticate to Unito.');
command.logToStderr(`Make sure to run the ${chalk_1.default.yellowBright('login')} command first.`);
}
else if (error instanceof EntryDecryptionError) {
command.logToStderr();
command.logToStderr(chalk_1.default.redBright('A secret cannot be decrypted!'));
command.logToStderr(`The secret ${chalk_1.default.yellowBright(error.key)} could not be decrypted.`);
command.logToStderr();
command.logToStderr(`To decrypt this secret locally, make sure:`);
command.logToStderr();
command.logToStderr(`1. The integration was deployed once with the ${chalk_1.default.yellowBright('publish')} command.`);
command.logToStderr('2. You have access to the integration.');
command.logToStderr(`3. The secret was encrypted for the ${chalk_1.default.yellowBright(error.environment)} environment.`);
}
else if (error instanceof MissingAuth2AuthorizationError) {
command.logToStderr();
command.logToStderr(chalk_1.default.redBright(`No Oauth2 authorization found in your configuration file`));
command.logToStderr('See https://dev.unito.io/docs/connectors/configuration/authorizations#oauth-2 for more information on how to configure Oauth2');
}
else if (error instanceof CredentialAuthenticationError) {
command.logToStderr();
command.logToStderr(chalk_1.default.redBright('The credential cannot be obtained!'));
command.logToStderr('To obtain information on the credential, you must authenticate to Unito.');
command.logToStderr(`Make sure to run the ${chalk_1.default.yellowBright('login')} command first.`);
}
else if (error instanceof CredentialAccessDenied) {
command.logToStderr();
command.logToStderr(chalk_1.default.redBright('The credential cannot be obtained!'));
command.logToStderr("You don't have access to this credential or the credential doesn't exist.");
}
else if (error instanceof IntegrationAlreadyExistsError) {
command.logToStderr();
command.logToStderr(chalk_1.default.redBright(`The integration already exists in this folder.`));
command.logToStderr(`Make sure to provide a unique name for your integration.`);
}
else {
unhandled = true;
}
return !unhandled;
}