Skip to content
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

Adds AfterError callback hook #6649

Conversation

guilhermefbarbosa
Copy link

@guilhermefbarbosa guilhermefbarbosa commented Oct 19, 2023

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

This pull request adds an new callback hook: AfterError that should run if any other main callback returns an error.

Currently, AfterXYZ hooks like AfterCreate, AfterSave and AfterUpdate doesn't get executed when an error is found, this leads to some unnecessary (but not always a problem) levels of code coupling between applications and errors returned by gorm.

User Case Description

There are many possible use cases, but mainly:

  1. Use AfterError hooks to translate/wrap gorm and database errors to application domain errors, decoupling other application layers from duty of knowing external errors.
  2. Use AfterError to handle specific table-wise errors, like UniqueIndex errors to redirect application flow

@guilhermefbarbosa guilhermefbarbosa marked this pull request as draft October 19, 2023 13:31
@guilhermefbarbosa guilhermefbarbosa changed the title Adds AfterError callback Adds AfterError callback hook Oct 19, 2023
@guilhermefbarbosa
Copy link
Author

i'm looking over these test failures

@guilhermefbarbosa guilhermefbarbosa marked this pull request as ready for review October 19, 2023 13:49
Copy link
Member

@a631807682 a631807682 left a comment

Choose a reason for hiding this comment

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

What is the difference between this feat and https://gorm.io/docs/write_plugins.html#Register-Callback-with-orders

@jinzhu
Copy link
Member

jinzhu commented Oct 26, 2023

Doesn't seem necessary for me.

@jinzhu jinzhu closed this Oct 26, 2023
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.

3 participants