From d8de1bcd9126e848eb3f43fd988dfa0091bfcc27 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 24 Aug 2017 11:58:51 -0700 Subject: [PATCH] secrets: treat any 4xx error code as "user error" (#744) The Secrets endpoints used to inappropriately return a 400 error code when a secret name collision occurred (i.e. when creating a secret with a name that already exists, or when trying to rename a secret to a name that is already in use). Now they return 409 Conflict which is more correct for those scenarios. So while we're at it, just treat all 4xx error codes as user errors. --- lib/secrets.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/secrets.js b/lib/secrets.js index 4fe9f1d..898a60c 100644 --- a/lib/secrets.js +++ b/lib/secrets.js @@ -1,6 +1,8 @@ // Ours const Now = require('../lib') +const isUserError = res => ((res.status / 100) | 0) === 4 + module.exports = class Secrets extends Now { ls() { return this.listSecrets() @@ -27,7 +29,7 @@ module.exports = class Secrets extends Now { const body = await res.json() if (res.status !== 200) { - if (res.status === 404 || res.status === 400) { + if (isUserError(res)) { const err = new Error(body.error.message) err.userError = true return bail(err) @@ -65,7 +67,7 @@ module.exports = class Secrets extends Now { const body = await res.json() if (res.status !== 200) { - if (res.status === 404 || res.status === 400) { + if (isUserError(res)) { const err = new Error(body.error.message) err.userError = true return bail(err) @@ -102,7 +104,7 @@ module.exports = class Secrets extends Now { const body = await res.json() if (res.status !== 200) { - if (res.status === 404 || res.status === 400) { + if (isUserError(res)) { const err = new Error(body.error.message) err.userError = true return bail(err)