-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v11.0.6 handle 4xx errors gracefully
- Loading branch information
1 parent
1462281
commit f297eb2
Showing
2 changed files
with
24 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
import request from 'superagent' | ||
|
||
export function getJSON({ uri }, callback) { | ||
request.get(uri) | ||
.end((error, response) => { | ||
if (error) { | ||
callback(error) | ||
request.get(uri).end((error, response) => { | ||
if (error) { | ||
callback(error) | ||
return | ||
} | ||
if (response.clientError) { | ||
callback(null, {}) | ||
return | ||
} | ||
if (!response.ok) { | ||
callback(new Error('Invalid Response')) | ||
return | ||
} | ||
const { body, text } = response | ||
let jsonResponse = body | ||
if (!body && text) { | ||
try { | ||
jsonResponse = JSON.parse(text) | ||
} catch (error) { | ||
callback(new Error('Unable to Parse JSON')) | ||
return | ||
} | ||
if (!response.ok) { | ||
callback(new Error('Invalid Response')) | ||
return | ||
} | ||
const { body, text } = response | ||
let jsonResponse = body | ||
if (!body && text) { | ||
try { | ||
jsonResponse = JSON.parse(text) | ||
} catch (error) { | ||
callback(new Error('Unable to Parse JSON')) | ||
return | ||
} | ||
} | ||
callback(null, jsonResponse) | ||
}) | ||
} | ||
callback(null, jsonResponse) | ||
}) | ||
} |