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

hotfix: basic error handling #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andrelip
Copy link

@andrelip andrelip commented Nov 8, 2024

No description provided.

Comment on lines +45 to +54
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

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

Choose a reason for hiding this comment

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

tagging @tlemens

Copy link
Owner

@sagastume sagastume Nov 8, 2024

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.

Copy link
Owner

@sagastume sagastume Nov 8, 2024

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:

  1. https://github.com/whatyouhide/redix/blob/main/lib/redix/exceptions.ex
  2. https://web.archive.org/web/20180414015950/http://michal.muskala.eu/2017/02/10/error-handling-in-elixir-libraries.html#comments-whatyouhide-errors
  3. https://leandrocp.com.br/2020/08/leveraging-exceptions-to-handle-errors-in-elixir/

@sagastume
Copy link
Owner

Now that I remember, there’s a logging module here:
#https://github.com/sagastume/boltx/blob/master/lib/boltx/utils/logger.ex

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.

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