From 7bcea8e19defe26863b9f53209caef3f0f00b5d5 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Mon, 30 May 2016 10:57:36 -0700 Subject: [PATCH] alias: handle 409 --- lib/alias.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/alias.js b/lib/alias.js index fd00ce2..4708461 100644 --- a/lib/alias.js +++ b/lib/alias.js @@ -144,8 +144,12 @@ export default class Alias extends Now { await this.verifyOwnership(alias); } - await this.createAlias(depl, alias); - console.log(`${chalk.cyan('> Success!')} Deployment ${chalk.bold(`https://${depl.url}`)} ${chalk.dim(`(${depl.uid})`)} now points to ${chalk.bold(chalk.underline(`https://${alias}`))}`); + const { created, uid } = await this.createAlias(depl, alias); + if (created) { + console.log(`${chalk.cyan('> Success!')} Alias created ${chalk.dim(`(${uid})`)}: ${chalk.bold(`https://${depl.url}`)} ${chalk.dim(`(${depl.uid})`)} now points to ${chalk.bold(chalk.underline(`https://${alias}`))}`); + } else { + console.log(`${chalk.cyan('> Success!')} Alias already exists ${chalk.dim(`(${uid})`)}.`); + } } createAlias (depl, alias) { @@ -156,11 +160,12 @@ export default class Alias extends Now { body: { alias } }); - if (304 === res.status) return; - const body = await res.json(); if (this._debug) console.timeEnd(`> [debug] /now/deployments/${depl.uid}/aliases #${attempt}`); + // 409 conflict is returned if it already exists + if (409 === res.status) return { uid: body.error.uid }; + // no retry on authorization problems if (403 === res.status) { const code = body.error.code; @@ -204,6 +209,8 @@ export default class Alias extends Now { if (200 !== res.status && 304 !== res.status) { throw new Error('Unhandled error'); } + + return body; }); }