jovotech / jovo-framework

🔈 The React for Voice and Chat: Build Apps for Alexa, Messenger, Instagram, the Web, and more
https://www.jovo.tech
Apache License 2.0
1.67k stars 310 forks source link

:bug: Add missing non JSON ask error handling #1609

Closed sadlowskij closed 6 months ago

sadlowskij commented 6 months ago

Proposed Changes

Provoking an error in alexa ask deployment sometimes results in an error in the getAskError function.

Example: Using a non existent ask profile like so:

new AlexaCli({askProfile: 'does-not-exist', locales: {en: ['en-US']}})

This leads to the stderr:

Command failed: ask smapi create-upload-url -p does-not-exist
Debugger listening on ws://127.0.0.1:42595/25792652-dbcc-412d-bf9d-4db5fcbd8620
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Debugger listening on ws://127.0.0.1:39601/8953403c-efec-4e66-a3c0-89661f15f337
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
[Error]: Can't resolve profile [does-not-exist] as it doesn't exist. Please run "ask configure --profile does-not-exist" first.

The current version of getAskError tries to parse the substring after [Error], in this case this text Can't resolve profile [does-not-exist] as it doesn't exist. Please run "ask configure --profile does-not-exist" first.. As this clearly is no JSON object/array. This JSON.parse throws an error resulting in this being displayed after running jovo deploy:platform alexa:

🚀 Deploying Alexa Skill
  ✖ Uploading skill package

x Error: --------------------------------------------------------------------------------
›                                                                                 
› Message:
›  Unexpected token C in JSON at position 0
›                                                                                 
› Module:
›  JovoCliCore
›                                                                                 
›                                                                                 
› If you think this is not on you, you can submit an issue here: https://github.com/jovotech/jovo-cli/issues.

This is why I built a block to catch the error from JSON.parse, which creates the JovoCliError in a different way, leading to the actual helpful error message being displayed after running the jovo deploy, in this case:

🚀 Deploying Alexa Skill
  ✖ Uploading skill package

x Error: --------------------------------------------------------------------------------
›                                                                                 
› Message:
›  smapiCreateUploadUrl: Can't resolve profile [does-not-exist] as it doesn't exist. Please run "ask configure --profile does-not-exist" first.
›  
›                                                                                 
› Module:
›  AlexaCli
›                                                                                 
›                                                                                 
› If you think this is not on you, you can submit an issue here: https://github.com/jovotech/jovo-cli/issues.

As this further increased the size of the function I also refactored some logic into its own function getViolation to improve readability.

Types of Changes

Checklist

aswetlow commented 6 months ago

Thank you, Joshua!