-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
hotfix: basic error handling #105
base: master
Are you sure you want to change the base?
Conversation
def message(%__MODULE__{code: code, module: module} = error) do | ||
if code == :syntax_error do | ||
""" | ||
Syntax error | ||
#{error.bolt.message} | ||
""" | ||
else | ||
inspect(error, pretty: true) | ||
end | ||
# TODO: move to module.format_error(code) 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.
maybe?
def message(%__MODULE__{code: :syntax_error} = error) do
"""
Syntax error
#{error.bolt.message}
"""
end
def message(error) do
inspect(error, pretty: true)
# TODO: move to module.format_error(code) later
end
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.
tagging @tlemens
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’d like to add more context about this module to serve as documentation.
This module was created to manage and structure errors consistently across the system, improving the clarity of error messages.
The main purpose of this module is to provide a consistent format for errors that may arise from different sources within the library. For example:
- Bolt protocol errors: will contain specific information in the
:bolt field
. - PackStream errors: will include relevant details in the
:packstream field
.
This unified structure allows handling errors from various sources in a single format.
Example:
defmodule MyApp.Example do
def has_permissions(attrs) do
Boltx.Error.wrap(__MODULE__, :insufficient_permissions)
end
def format_message(:insufficient_permissions), do: "Unable to perform action due to insufficient permissions."
end
message Function
The message/1
function is an auxiliary function that formats errors in a more user-friendly and readable way. Its purpose is to transform error information into a message that could even be presented to an end user, if needed. This approach is similar to how the Redix
library.
Using Error.wrap and format_error
Each module that emits an error with Error.wrap should implement a format_error/1
function. This function is responsible for generating a user-friendly message specific to each type of error emitted by that module.
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.
my recommendation for generic errors, such as :syntax_error
, a format_error/1
function could be added to Boltx.Error, eliminating the need to define format_error in each module that issues such errors. At present, if I recall correctly, the error :syntax_error
can occur in several modules.
def module Boltx.Error
defp format_error(:syntax_error), do: "The query contains an invalid syntax"
end
References that could be useful:
- https://github.com/whatyouhide/redix/blob/main/lib/redix/exceptions.ex
- https://web.archive.org/web/20180414015950/http://michal.muskala.eu/2017/02/10/error-handling-in-elixir-libraries.html#comments-whatyouhide-errors
- https://leandrocp.com.br/2020/08/leveraging-exceptions-to-handle-errors-in-elixir/
Now that I remember, there’s a logging module here: It's used in client-server message comunication and can be activated like this in the config. config :logger, level: :debug
config :boltx,
log: true,
log_hex: false We could probably improve the logger to include it here. |
No description provided.