-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: overhauling error handling to support the new api error responses #218
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this error message because if a user tries to use
rdme docs
with a directory that has subdirectories containing Markdown, we currently can't handle those Markdown files asrdme docs
can't handle category creation and management right now.https://app.asana.com/0/1178132843162889/1183431246035890/f