Nexmo / nexmo-cli

Nexmo CLI (Command Line Interface)
https://nexmo.com
MIT License
78 stars 52 forks source link

Handle non 2xx status codes from Nexmo API #118

Closed adambutler closed 7 years ago

adambutler commented 7 years ago

When the Nexmo API responds with a non 2xx status code an appropriate response should be printed.

Perhaps something like:

Nexmo API is currently experiencing problems. Please see https://status.nexmo.com for more
information. If your problem persists please contact support at https://help.nexmo.com
screen_shot_2017-01-30_at_11_17_23
cbetta commented 7 years ago

This is an error in the Node library. Closing and I'll re-open it there.

cbetta commented 7 years ago

Actually I am wrong. This is an issue here. We're not catching the error thrown somehow.

cbetta commented 7 years ago

Ok seems the problem is with the Node library after all: rather than it returning an error object in this instance it's just returning a string with the HTML response.

cbetta commented 7 years ago

@adambutler this has now been fixed. Working to deploy this soon.

nexmo app:create 'File Test 1' 'http://c.betta.
io/answer' 'http://c.betta.io/event'

The API response could not be parsed.

Or:

nexmo app:create 'File Test 1' 'http://c.betta.io/answer' 'http://c.betta.io/event' --debug
info: Request: { host: 'api.nexmo.com',
  port: 443,
  path: '/v1/applications?name=File%20Test%201&type=voice&answer_url=http://c.betta.io/answer&event_url=http://c.betta.io/event&api_key=1234&api_secret=1234',
  method: 'POST',
  headers: 
   { 'Content-Type': 'application/x-www-form-urlencoded',
     Accept: 'application/json',
     'User-Agent': 'nexmo-node/1.1.1 node/6.7.0 nexmo-cli/0.3.1' } } 
Body: undefined
info: response ended: 504
error: SyntaxError: Unexpected token < in JSON at position 0
    at Object.parse (native)
    at IncomingMessage.<anonymous> (/Users/cbetta/.nvm/versions/node/v6.7.0/lib/node_modules/nexmo-cli/node_modules/nexmo/lib/HttpClient.js:90:48)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
error: could not convert API response to JSON, above error is ignored and raw API response is returned to client
error: Raw Error message from API 
error: <html>
<head><title>504 Gateway Time-out</title></head>
<body bgcolor="white">
<center><h1>504 Gateway Time-out</h1></center>
<hr><center>nginx</center>
</body>
</html>

Validator.response() - Error: 
{ message: 'The API response could not be parsed.',
  parseError: 
   SyntaxError: Unexpected token < in JSON at position 0
       at Object.parse (native)
       at IncomingMessage.<anonymous> (/Users/cbetta/.nvm/versions/node/v6.7.0/lib/node_modules/nexmo-cli/node_modules/nexmo/lib/HttpClient.js:90:48)
       at emitNone (events.js:91:20)
       at IncomingMessage.emit (events.js:185:7)
       at endReadableNT (_stream_readable.js:974:12)
       at _combinedTickCallback (internal/process/next_tick.js:74:11)
       at process._tickCallback (internal/process/next_tick.js:98:9) }
Validator.response() - Response: 
null
End Validator.response()
The API response could not be parsed.
cbetta commented 7 years ago

I've also added a specific error for errors with codes >= 500

> nexmo app:create 'File Test 1' 'http://c.betta.io/answer' 'http://c.betta.io/event'
Server Error: 504