Apical.ToJson protocol (apical v0.3.0)

Copy Markdown View Source

Protocol for converting Apical errors to JSON-compatible maps.

This protocol provides a consistent way to convert Apical exceptions to maps that can be easily serialized to JSON for API error responses.

Example

error = %Apical.Exceptions.ParameterError{
  operation_id: "getUser",
  in: :path,
  reason: "required parameter `id` not present"
}

json_map = Apical.ToJson.to_json(error)
# => %{
#   error: "parameter_error",
#   status: 400,
#   operation_id: "getUser",
#   location: "path",
#   message: "Parameter Error in operation getUser (in path): ..."
# }

Jason.encode!(json_map)
# => "{"error":"parameter_error","status":400,...}"

Summary

Types

t()

All the types that implement this protocol.

Functions

Converts an Apical exception to a JSON-compatible map.

Types

t()

@type t() :: term()

All the types that implement this protocol.

Functions

to_json(error)

@spec to_json(t()) :: map()

Converts an Apical exception to a JSON-compatible map.

Returns a map with at minimum:

  • :error - A string identifier for the error type
  • :status - The HTTP status code
  • :message - A human-readable error message

Additional fields may be included depending on the error type.