-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
add expectedError method #6581
add expectedError method #6581
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,15 +179,44 @@ export class Log { | |
* asynchronously. | ||
* @param {string} tag | ||
* @param {...*} var_args | ||
* @return {!Error|undefined} | ||
* @private | ||
*/ | ||
error(tag, var_args) { | ||
error_(tag, var_args) { | ||
if (this.level_ >= LogLevel.ERROR) { | ||
this.msg_(tag, 'ERROR', Array.prototype.slice.call(arguments, 1)); | ||
} else { | ||
const error = createErrorVargs.apply(null, | ||
Array.prototype.slice.call(arguments, 1)); | ||
this.prepareError_(error); | ||
this.win.setTimeout(() => {throw error;}); | ||
return error; | ||
} | ||
} | ||
|
||
/** | ||
* Reports an error message. | ||
* @param {string} tag | ||
* @param {...*} var_args | ||
* @return {!Error|undefined} | ||
*/ | ||
error(tag, var_args) { | ||
const error = this.error_.apply(this, arguments); | ||
if (error) { | ||
this.win.setTimeout(() => {throw /** @type {!Error} */ (error);}); | ||
} | ||
} | ||
|
||
/** | ||
* Reports an error message and marks with an expected property. If the | ||
* logging is disabled, the error is rethrown asynchronously. | ||
* @param {string} tag | ||
* @param {...*} var_args | ||
*/ | ||
expectedError(tag, var_args) { | ||
const error = this.error_.apply(this, arguments); | ||
if (error) { | ||
error.expected = true; | ||
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. While correct, it's a bit weird to set flag on an error after it's been reported. Maybe better refactor call into a private method and call from both methods with different flags? 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. yeah i can do that, that was actually the original code i had written. 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. done. |
||
this.win.setTimeout(() => {throw /** @type {!Error} */ (error);}); | ||
} | ||
} | ||
|
||
|
@@ -202,6 +231,18 @@ export class Log { | |
return error; | ||
} | ||
|
||
/** | ||
* Creates an error object with its expected property set to true. | ||
* @param {...*} var_args | ||
* @return {!Error} | ||
*/ | ||
createExpectedError(var_args) { | ||
const error = createErrorVargs.apply(null, arguments); | ||
this.prepareError_(error); | ||
error.expected = true; | ||
return error; | ||
} | ||
|
||
/** | ||
* Throws an error if the first argument isn't trueish. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -353,7 +353,7 @@ export function fetchPolyfill(input, opt_init) { | |
} | ||
if (xhr.status < 100 || xhr.status > 599) { | ||
xhr.onreadystatechange = null; | ||
reject(new Error(`Unknown HTTP status ${xhr.status}`)); | ||
reject(user().createExpectedError(`Unknown HTTP status ${xhr.status}`)); | ||
return; | ||
} | ||
|
||
|
@@ -365,10 +365,10 @@ export function fetchPolyfill(input, opt_init) { | |
} | ||
}; | ||
xhr.onerror = () => { | ||
reject(new Error('Network failure')); | ||
reject(user().createExpectedError('Network failure')); | ||
}; | ||
xhr.onabort = () => { | ||
reject(new Error('Request aborted')); | ||
reject(user().createExpectedError('Request aborted')); | ||
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. @dvoytenko please review |
||
}; | ||
|
||
if (init.method == 'POST') { | ||
|
@@ -394,7 +394,7 @@ function createXhrRequest(method, url) { | |
xhr = new XDomainRequest(); | ||
xhr.open(method, url); | ||
} else { | ||
throw new Error('CORS is not supported'); | ||
throw dev().createExpectedError('CORS is not supported'); | ||
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. @dvoytenko can you review this? |
||
} | ||
return xhr; | ||
} | ||
|
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.
Mark private.
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.
done.