Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🥅 Improve error handling #232

Merged
merged 6 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions commands/command-build/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeployEvents } from '@jovotech/cli-command-deploy';
import { DeployCodeEvents, DeployPlatformEvents } from '@jovotech/cli-command-deploy';
import {
checkForProjectDirectory,
CliFlags,
Expand Down Expand Up @@ -32,7 +32,7 @@ export interface BuildContext extends PluginContext {

export type BuildEvents = 'before.build' | 'build' | 'after.build' | 'reverse.build';

export class Build extends PluginCommand<BuildEvents | DeployEvents> {
export class Build extends PluginCommand<BuildEvents | DeployCodeEvents | DeployPlatformEvents> {
static id = 'build';
static description = 'Build platform-specific language models based on jovo models folder.';
static examples: string[] = ['jovo build --platform alexaSkill', 'jovo build --target zip'];
Expand Down Expand Up @@ -160,9 +160,9 @@ export class Build extends PluginCommand<BuildEvents | DeployEvents> {

if (flags.deploy) {
Log.spacer();
await this.$emitter.run('before.deploy');
await this.$emitter.run('deploy');
await this.$emitter.run('after.deploy');
await this.$emitter.run('before.deploy:platform');
await this.$emitter.run('deploy:platform');
await this.$emitter.run('after.deploy:platform');
}

Log.spacer();
Expand Down
56 changes: 0 additions & 56 deletions commands/command-deploy/src/commands/deploy.ts

This file was deleted.

4 changes: 1 addition & 3 deletions commands/command-deploy/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { JovoCliPlugin, PluginCommand, PluginType } from '@jovotech/cli-core';
import { Deploy } from './commands/deploy';
import { DeployCode } from './commands/deploy.code';
import { DeployPlatform } from './commands/deploy.platform';

export * from './commands/deploy';
export * from './commands/deploy.code';
export * from './commands/deploy.platform';

Expand All @@ -12,7 +10,7 @@ export class DeployCommand extends JovoCliPlugin {
$type: PluginType = 'command';

getCommands(): typeof PluginCommand[] {
return [Deploy, DeployCode, DeployPlatform];
return [DeployCode, DeployPlatform];
}
}

Expand Down
2 changes: 1 addition & 1 deletion commands/command-get/src/commands/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface GetContext extends PluginContext {

export class Get extends PluginCommand<BuildEvents | GetEvents> {
static id = 'get';
static description = 'Downloads an existing platform project into the platforms folder.';
static description = 'Downloads an existing platform project into the build folder.';
static examples: string[] = [
'jovo get alexaSkill --skill-id amzn1.ask.skill.xxxxxxxx',
'jovo get googleAction --project-id testproject-xxxxxx',
Expand Down
8 changes: 6 additions & 2 deletions core/src/Project.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join as joinPaths, sep as pathSeperator } from 'path';
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'fs';
import tv4 from 'tv4';
import { JovoModelData, ModelValidationError } from '@jovotech/model';
import { JovoModelData } from '@jovotech/model';

import { JovoCliError } from './JovoCliError';
import { Config } from './Config';
Expand Down Expand Up @@ -133,7 +133,11 @@ export class Project {
const model: JovoModelData = this.getModel(locale);

if (!tv4.validate(model, validator)) {
throw new ModelValidationError(tv4.error.message, locale, tv4.error.dataPath);
throw new JovoCliError({
message: `Validation failed for locale "${locale}"`,
details: tv4.error.message,
learnMore: tv4.error.dataPath ? `Path: ${tv4.error.dataPath}` : '',
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class Task {
throw error;
}

throw new JovoCliError(error.message);
throw new JovoCliError({ message: error.message });
}
}
}
Expand Down
31 changes: 29 additions & 2 deletions core/src/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import prompt from 'prompts';
import { ANSWER_BACKUP, ANSWER_CANCEL, ANSWER_OVERWRITE } from './constants';
import { printUserInput } from './prints';
import { printHighlight, printUserInput } from './prints';

/**
* Prompt if existing model files should be overwritten.
Expand Down Expand Up @@ -69,9 +69,36 @@ export async function promptOverwrite(message: string): Promise<{ overwrite: str
],
},
{
onCancel(prompt, answers) {
onCancel(_prompt, answers) {
answers.overwrite = ANSWER_CANCEL;
},
},
);
}

export async function promptSupportedLocales(
locale: string,
platform: string,
supportedLocales: string[],
): Promise<{ locales: string[] }> {
return await prompt(
{
name: 'locales',
type: 'multiselect',
message: `Locale ${printHighlight(
locale,
)} is not supported by ${platform}.\nPlease provide an alternative locale (type to filter, select with space):`,
instructions: false,
min: 1,
choices: supportedLocales.map((locale) => ({
title: printUserInput(locale),
value: locale,
})),
},
{
onCancel() {
process.exit();
},
},
);
}
4 changes: 3 additions & 1 deletion core/test/Project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ describe('validateModel()', () => {
tv4.error = { message: 'Validation failed.' };

const project: Project = new Project('');
expect(project.validateModel.bind(project, 'en', {})).toThrow('Validation failed.');
expect(project.validateModel.bind(project, 'en', {})).toThrow(
'Validation failed for locale "en"',
);
});

test('should do nothing if model is valid', () => {
Expand Down
Loading