@ -14,6 +14,7 @@ const retry = require('async-retry')
const splitArray = require ( 'split-array' )
const { parse : parseIni } = require ( 'ini' )
const { readFile , stat , lstat } = require ( 'fs-extra' )
const { responseError } = require ( './error' )
// Ours
const {
@ -526,12 +527,24 @@ module.exports = class Now extends EventEmitter {
}
async listAliases ( deploymentId ) {
return this . retry ( async ( ) => {
return this . retry ( async bail => {
const res = await this . _ fetch (
deploymentId
? ` /now/deployments/ ${ deploymentId } /aliases `
: '/now/aliases'
)
if ( res . status >= 400 && res . status < 500 ) {
if ( this . _ debug ) {
console . log ( '> [debug] bailing on get domain due to %s' , res . status )
}
return bail ( await responseError ( res ) )
}
if ( res . status !== 200 ) {
throw new Error ( 'API error getting aliases' )
}
const body = await res . json ( )
return body . aliases
} )
@ -567,6 +580,17 @@ module.exports = class Now extends EventEmitter {
console . timeEnd ( ` > [debug] # ${ attempt } GET /domains ` )
}
if ( res . status >= 400 && res . status < 500 ) {
if ( this . _ debug ) {
console . log ( '> [debug] bailing on get domain due to %s' , res . status )
}
return bail ( await responseError ( res ) )
}
if ( res . status !== 200 ) {
throw new Error ( 'API error getting domains' )
}
const body = await res . json ( )
return body . domains
} )
@ -580,6 +604,17 @@ module.exports = class Now extends EventEmitter {
const res = await this . _ fetch ( ` /domains/ ${ domain } ` )
if ( res . status >= 400 && res . status < 500 ) {
if ( this . _ debug ) {
console . log ( '> [debug] bailing on get domain due to %s' , res . status )
}
return bail ( await responseError ( res ) )
}
if ( res . status !== 200 ) {
throw new Error ( 'API error getting domain name' )
}
if ( this . _ debug ) {
console . timeEnd ( ` > [debug] # ${ attempt } GET /domains/ ${ domain } ` )
}
@ -953,41 +988,6 @@ function toRelative(path, base) {
return relative . replace ( /\\/g , '/' )
}
async function responseError ( res ) {
let message
let userError
if ( res . status >= 400 && res . status < 500 ) {
let body
try {
body = await res . json ( )
} catch ( err ) {
body = { }
}
// Some APIs wrongly return `err` instead of `error`
message = ( body . error || body . err || { } ) . message
userError = true
} else {
userError = false
}
const err = new Error ( message || 'Response error' )
err . status = res . status
err . userError = userError
if ( res . status === 429 ) {
const retryAfter = res . headers . get ( 'Retry-After' )
if ( retryAfter ) {
err . retryAfter = parseInt ( retryAfter , 10 )
}
}
return err
}
function hasNpmStart ( pkg ) {
return pkg . scripts && ( pkg . scripts . start || pkg . scripts [ 'now-start' ] )
}