Skip to content

[Updater] Break out handler methods for Dependabot::Error classes into a small utility class#6865

Merged
brrygrdn merged 1 commit intomainfrom
brrygrdn/extract-error-handler
Mar 20, 2023
Merged

[Updater] Break out handler methods for Dependabot::Error classes into a small utility class#6865
brrygrdn merged 1 commit intomainfrom
brrygrdn/extract-error-handler

Conversation

@brrygrdn
Copy link
Copy Markdown
Contributor

Follows up on #6847 which must merge first

This is small and somewhat imperfect extract-to-class refactor to move aside some long methods we use for presenting Dependabot::Errors via the Dependabot::Service and/or logging.

As we split out distinct operator classes, duplicating these methods is really undesirable as they have a lot of specific behaviour that would be hard to keep in alignment.

I think there's a deeper refactor to be had here since methods ultimately derive an "error detail" hash based on the subclass of Dependabot::Error being handled, e.g.

{
  "error-type": "private_source_timed_out",
  "error-detail": { source: error.source }
}

I think this could just become a method on each Dependabot::Error subclass, but that would require a significant change in core with substantial testing to be confident.

Likewise, I think the handle_dependabot_error and handle_parser_error could be DRY'd out, but the parsing itself should be pulled up out of the Updater so doing a deeper change at this time might be counter-productive.

@brrygrdn brrygrdn requested a review from a team as a March 17, 2023 18:31
@brrygrdn brrygrdn force-pushed the brrygrdn/extract-dependency-parsing branch from 9baa4df to 6732de2 Compare March 17, 2023 18:32
@brrygrdn brrygrdn force-pushed the brrygrdn/extract-error-handler branch from a0c7ce7 to a13a794 Compare March 17, 2023 18:33
@brrygrdn brrygrdn force-pushed the brrygrdn/extract-dependency-parsing branch from 6732de2 to f8f2fa2 Compare March 20, 2023 12:01
Copy link
Copy Markdown
Contributor

@landongrindheim landongrindheim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤩

Comment on lines 52 to 68
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Would it make sense to expose a method that signals that parsing has completed? I understand defining @dependencies signals this internally, and that we may not want to have collaborators checking this externally. Maybe something like a result object would be appropriate, given your note about handling parse errors.

None of this is necessary, but maybe it would be useful? I'm interested in your thoughts as we're still paving the path for further changes.

Copy link
Copy Markdown
Contributor Author

@brrygrdn brrygrdn Mar 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may not want to have collaborators checking this externally.

I think the call order we have right now means we can proceed if parsing fails, but from reasoning through the code I don't think we ever actually should? i.e. there shouldn't be any collaboration if parsing fails, it's always a sign to stop the job.

if job.updating_a_pull_request?
Dependabot.logger.info("Starting PR update job for #{job.source.repo}")
check_and_update_existing_pr_with_error_handling(dependencies)
else
Dependabot.logger.info("Starting update job for #{job.source.repo}")
dependencies.each { |dep| check_and_create_pr_with_error_handling(dep) }
end

In the existing behaviour, if parsing fails, we get an empty value for "dependencies" at the code above.

For a new update, [] means we don't iterate and the job silently ends.

For updating an existing PR, we end up here:

if dependencies.count != job.dependencies.count
# TODO: Remove the unless statement
#
# This check is to determine if there was an error parsing the dependency
# dependency file.
#
# For update existing PR operations we should early exit after a failed
# parse instead, but we currently share the `#dependencies` method
# with other code paths. This will be fixed as we break out Operation
# classes.
close_pull_request(reason: :dependency_removed) unless service.errors.any?
return
end

This will nearly always result in [] != [$at_least_one_dep], so we exit. We don't close the PR as the parser failure has placed an error on the job, but we could have the same outcome by stopping if the parse fails.

This is feels an awful lot like cleanly closing the job accidentally/on a weak signal. If we stop immediately when parsing fails, it isn't actually a behaviour change, but it does mean reshuffling a lot of tests around so I'm reluctant to follow through for now.

Comment on lines 15 to 22
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a no-op of sorts, probably in the shape of error forwarding, makes sense here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This feels like a solid place for this logic 🏡

Comment on lines 3 to 19
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm appreciating this as a place to put error handling. The reasons for it to change will be limited to new errors and new requirements to handle those errors. That feels right to me, especially compared to the previous location 🙂

Base automatically changed from brrygrdn/extract-dependency-parsing to main March 20, 2023 13:20
@brrygrdn brrygrdn force-pushed the brrygrdn/extract-error-handler branch from a13a794 to 870b293 Compare March 20, 2023 13:33
@brrygrdn brrygrdn merged commit e5bb099 into main Mar 20, 2023
@brrygrdn brrygrdn deleted the brrygrdn/extract-error-handler branch March 20, 2023 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants