-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: overhauling error handling to support the new api error respons…
…es (#218) * feat: overhauling error handling to support the new api error responses * test: fixing some broken tests
- Loading branch information
Showing
15 changed files
with
176 additions
and
107 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module.exports = class extends Error { | ||
constructor(res) { | ||
let err; | ||
|
||
// Special handling to for fetch `res` arguments where `res.error` will contain our API error response. | ||
if (typeof res === 'object') { | ||
if ('error' in res && typeof res.error === 'object') { | ||
err = res.error; | ||
} else { | ||
err = res; | ||
} | ||
} else { | ||
err = res; | ||
} | ||
|
||
super(err); | ||
|
||
if (typeof err === 'object') { | ||
this.code = err.error; | ||
|
||
// If we returned help info in the API, show it otherwise don't render out multiple empty lines as we sometimes | ||
// throw `Error('non-api custom error message')` instances and catch them with this class. | ||
if ('help' in err) { | ||
this.message = [err.message, '', err.help].join('\n'); | ||
} else { | ||
this.message = err.message; | ||
} | ||
|
||
this.name = 'APIError'; | ||
} else { | ||
this.code = err; | ||
this.message = err; | ||
} | ||
} | ||
}; |
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 |
---|---|---|
|
@@ -8,37 +8,17 @@ require('./cli')(process.argv.slice(2)) | |
}) | ||
.catch(e => { | ||
if (e) { | ||
// `err.message` is from locally thrown Error objects | ||
// `err.error` is from remote API errors | ||
const err = e; | ||
|
||
// If we've got a remote API error, extract its contents so we can show the user the error. | ||
if (typeof err.error === 'object' && Object.keys(err.error).length === 3) { | ||
err.message = err.error.error; | ||
err.description = err.error.description; | ||
err.errors = err.error.errors; | ||
} | ||
|
||
if (!err.description && !err.errors && err.error) { | ||
if ('message' in err) { | ||
console.error(err.message.red); | ||
} else { | ||
console.error( | ||
`Yikes, something went wrong! Please try again and if the problem persists, get in touch with our support team at ${ | ||
`[email protected]`.underline | ||
}.\n`.red | ||
); | ||
} | ||
|
||
if (err.message && (typeof err.statusCode === 'undefined' || err.statusCode !== 404)) | ||
console.error(err.message.red); | ||
|
||
if (err.description) console.error(err.description.red); | ||
if (err.errors) { | ||
const errors = Object.keys(err.errors); | ||
|
||
console.error(`\nCause${(errors.length > 1 && 's') || ''}:`.red.bold); | ||
errors.forEach(error => { | ||
console.error(` · ${error}: ${err.errors[error]}`.red); | ||
}); | ||
} | ||
} | ||
|
||
return process.exit(1); | ||
|
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 |
---|---|---|
|
@@ -118,8 +118,10 @@ describe('rdme docs', () => { | |
.get(`/api/v1/docs/${slug}`) | ||
.basicAuth({ user: key }) | ||
.reply(404, { | ||
description: 'No doc found with that slug', | ||
error: 'Not Found', | ||
error: 'DOC_NOTFOUND', | ||
message: `The doc with the slug '${slug}' couldn't be found`, | ||
suggestion: '...a suggestion to resolve the issue...', | ||
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".', | ||
}); | ||
|
||
const postMock = nock(config.host, { | ||
|
@@ -211,12 +213,17 @@ describe('rdme docs:edit', () => { | |
|
||
const getMock = nock(config.host) | ||
.get(`/api/v1/docs/${slug}`) | ||
.reply(404, { error: 'Not Found', description: 'No doc found with that slug' }); | ||
.reply(404, { | ||
error: 'DOC_NOTFOUND', | ||
message: `The doc with the slug '${slug}' couldn't be found`, | ||
suggestion: '...a suggestion to resolve the issue...', | ||
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".', | ||
}); | ||
|
||
return docsEdit.run({ slug, key, version: '1.0.0' }).catch(err => { | ||
getMock.done(); | ||
expect(err.error).toBe('Not Found'); | ||
expect(err.description).toBe('No doc found with that slug'); | ||
expect(err.code).toBe('DOC_NOTFOUND'); | ||
expect(err.message).toContain("The doc with the slug 'no-such-doc' couldn't be found"); | ||
}); | ||
}); | ||
|
||
|
@@ -226,14 +233,19 @@ describe('rdme docs:edit', () => { | |
|
||
const getMock = nock(config.host).get(`/api/v1/docs/${slug}`).reply(200, {}); | ||
|
||
const putMock = nock(config.host).put(`/api/v1/docs/${slug}`).reply(400, { error: 'Bad Request' }); | ||
const putMock = nock(config.host).put(`/api/v1/docs/${slug}`).reply(400, { | ||
error: 'DOC_INVALID', | ||
message: "We couldn't save this doc ({error})", | ||
suggestion: '...a suggestion to resolve the issue...', | ||
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".', | ||
}); | ||
|
||
function mockEditor(filename, cb) { | ||
return cb(0); | ||
} | ||
|
||
return docsEdit.run({ slug, key, version: '1.0.0', mockEditor }).catch(err => { | ||
expect(err.error).toBe('Bad Request'); | ||
expect(err.code).toBe('DOC_INVALID'); | ||
getMock.done(); | ||
putMock.done(); | ||
expect(fs.existsSync(`${slug}.md`)).toBe(true); | ||
|
Oops, something went wrong.