Skip to content
This repository has been archived by the owner on Nov 14, 2019. It is now read-only.

Do not raise an error when a service is already started #131

Merged
merged 3 commits into from
Aug 22, 2019
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
19 changes: 12 additions & 7 deletions src/commands/service/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
}
24 changes: 24 additions & 0 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
@@ -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
}