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: http error handling without retries #226
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dc85dca
fix: do not reveal unexpected errors
vtsaplin 4303ea9
feat: set timeout back to normal value
vtsaplin 52eef6d
refactor: reaming
vtsaplin 8f3173a
refactor: better wording of error message
vtsaplin 66929be
refactor: apply review feedback
vtsaplin d3768a9
test: fix broken test
vtsaplin add5131
refactor: apply code review feedback
vtsaplin d199b35
fix: wrong import
vtsaplin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,15 +9,37 @@ | |
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
const AbortAddon = require('wretch/addons/abort'); | ||
|
||
const wretch = require('wretch'); | ||
const { retry } = require('wretch/middlewares/retry'); | ||
const { Core } = require('@adobe/aio-sdk'); | ||
|
||
const REQUEST_TIMEOUT = 55 * 1000; | ||
|
||
const logger = Core.Logger('FirefallAction'); | ||
|
||
class NetworkError extends Error { | ||
constructor(status, message) { | ||
super(message); | ||
this.name = 'NetworkError'; | ||
this.status = status; | ||
} | ||
} | ||
|
||
function wretchRetry(url) { | ||
function wretchWithOptions(url) { | ||
return wretch(url) | ||
.middlewares([retry({ | ||
retryOnNetworkError: true, | ||
until: (response) => response && (response.ok || (response.status >= 400 && response.status < 500)), | ||
})]); | ||
.addon(AbortAddon()) | ||
.resolve((_) => _.setTimeout(REQUEST_TIMEOUT)) | ||
.resolve((_) => { | ||
return _.fetchError((error) => { | ||
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. Generally, _ is used as a placeholder for unused parameter. However, using _ in this way creates confusion as to why a method is called on it. I would have preferred a parameter name such as |
||
if (error.name === 'AbortError') { | ||
logger.error('Request aborted', error); | ||
throw new NetworkError(408, 'Request timed out'); | ||
} | ||
logger.error('Network error', error); | ||
throw new NetworkError(500, 'Network error'); | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = { wretchRetry }; | ||
module.exports = { wretch: wretchWithOptions, NetworkError }; |
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
Oops, something went wrong.
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.
return response.valid
?