diff --git a/src/commands/service/start.ts b/src/commands/service/start.ts index 11486dc..01192e1 100644 --- a/src/commands/service/start.ts +++ b/src/commands/service/start.ts @@ -2,6 +2,7 @@ import {flags} from '@oclif/command' import {InstanceCreateOutputs} from 'mesg-js/lib/api' import Command from '../../root-command' +import {errorConversion} from '../../utils/error' import serviceResolver from '../../utils/service-resolver' export default class ServiceStart extends Command { @@ -25,12 +26,16 @@ export default class ServiceStart extends Command { const {args, flags} = this.parse(ServiceStart) this.spinner.start('Start instance') const serviceHash = await serviceResolver(this.api, args.SERVICE_HASH) - const instance = await this.api.instance.create({ - serviceHash, - env: flags.env - }) - if (!instance.hash) throw new Error('invalid instance') - this.spinner.stop(instance.hash) - return instance + try { + const instance = await this.api.instance.create({ + serviceHash, + env: flags.env + }) + if (!instance.hash) throw new Error('invalid instance') + this.spinner.stop(instance.hash) + return instance + } catch (err) { + throw errorConversion(err) + } } } diff --git a/src/utils/error.ts b/src/utils/error.ts new file mode 100644 index 0000000..ff4eb95 --- /dev/null +++ b/src/utils/error.ts @@ -0,0 +1,24 @@ +export class IsAlreadyExistsError extends Error { + static ID = 'ALREADY_EXISTS' + static regexp = new RegExp('\"(.*)\" already exists') + static match(err: Error) { + return IsAlreadyExistsError.regexp.test(err.message) + } + + hash: string + + constructor(error: Error) { + super(error.message) + const res = IsAlreadyExistsError.regexp.exec(error.message) + const hash = res && res.length >= 1 ? res[1] : '' + this.hash = hash + this.name = IsAlreadyExistsError.ID + } +} + +export const errorConversion = (err: Error): Error => { + if (IsAlreadyExistsError.match(err)) { + return new IsAlreadyExistsError(err) + } + return err +}