-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
[appveyor] Error handling in BaseService #1590
Changes from all commits
252d715
6918919
33c8d9e
5199a76
0a88d37
36f4f08
e44ffbb
877bdf8
16c79cb
b245ca9
180dcfc
f22a8d4
df6b23c
dfafdc0
b546b6f
f68e06f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,23 @@ | ||
'use strict'; | ||
|
||
const BaseService = require('../base'); | ||
const { | ||
checkErrorResponse, | ||
asJson, | ||
} = require('../../lib/error-helper'); | ||
|
||
/** | ||
* AppVeyor CI integration. | ||
*/ | ||
module.exports = class AppVeyor extends BaseService { | ||
async handle({repo, branch}) { | ||
let apiUrl = 'https://ci.appveyor.com/api/projects/' + repo; | ||
if (branch != null) { | ||
apiUrl += '/branch/' + branch; | ||
} | ||
const {buffer, res} = await this._sendAndCacheRequest(apiUrl, { | ||
const json = await this._sendAndCacheRequest(apiUrl, { | ||
headers: { 'Accept': 'application/json' } | ||
}); | ||
}).then(checkErrorResponse.asPromise({ notFoundMessage: 'project not found or access denied' })) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is already a good reduction in boilerplate, but I feel like we're very often going to chain .. something like: async _wrappedRequest(url, options, handler=checkErrorResponse.asPromise({})) {
return this._sendAndCacheRequest(url, options).then(handler);
} in If there is a situation where something really non-standard happens, we could always override There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, the vast majority of the time we're wanting to do the same thing here. Adding a method is a nice idea. Perhaps it could be For a custom not-found message, which is or should be relatively common, perhaps we could use a separate As you say, this could be overridden when something more custom is needed. I'd even go so far as to say that, perhaps, the method should be I'd prefer to have concerns a little more separated – having JSON-defaulting stuff in BaseService begins to cross the line for me – but we can revisit that later. With a few more examples, probably separating concerns will be a bit easier. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I had a very similar thought to this. Maybe this is another good way to look at this: class BaseJsonService extends BaseService and chain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! I'm good with that as a starting point. Though, down the line, I'd also like to consider breaking the vendor-calling code out of the service class. |
||
.then(asJson); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: If we want to throw an exception here in the try {
return await this.handle(namedParams);
} catch (error) {
...
} it seems like I should be able to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right! So, the way to signal an exception based on the response content is to That said, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right yes, so if I add the line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bit more info: that behaviour is only true with If I set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It took me a while to get clarity about what needed to happen here, though I think I've gotten it working well now. Would you like to take a look? |
||
|
||
if (res.statusCode === 404) { | ||
return {message: 'project not found or access denied'}; | ||
} | ||
|
||
const data = JSON.parse(buffer); | ||
const status = data.build.status; | ||
const { build: { status } } = json; | ||
if (status === 'success') { | ||
return {message: 'passing', color: 'brightgreen'}; | ||
} else if (status !== 'running' && status !== 'queued') { | ||
|
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.
Does this have to be async? It's not doing anything asynchronously.
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.
I think if it weren't async, it would not return a promise.