Skip to content

New and more flexible callbacks, and way more

Latest
Compare
Choose a tag to compare
@Cantido Cantido released this 23 Mar 20:11
· 31 commits to master since this release
cc39de0

Added

New Callbacks

  • The handle_error/3 handler has been added. This is a special handler that lets you manipulate the conn after an error is raised, so you can choose your status code and body yourself.
    (docs) (#33)
  • The maximum_entity_length/1 function. This is used by the valid_entity_length?/1 callback. If the size of the request body is above this size, Liberator will return a status of 413 Request Entity Too Large. (docs)
  • Added the well_formed?/1 function. This function checks the request for general adherence to some form, like if it is valid JSON. It replaces malformed?/1 so that you can return some data after you check for form. (docs)
  • Added the body_exists?/1 function. This is an internal function that, uh, checks to see if the body exists. If it does, it'll grab it, and call valid_entity_length?/1 and the new well_formed?/1 function. So now you can parse the body if it's there, without worrying about conditional logic if it's not there. (docs)

Additions to callbacks

  • You can now return the given conn in decision functions and actions to serve as an affirmative response, as an alternative to returning a plain map, or true. Now you can modify the conn as you see fit.
  • You can now return :ok or :error tuples from decisions, actions, and handlers. Returning {:error, term} will invoke the new handle_error/3 function.

Docs for callbacks

Added internationalization

  • Internationalization is now supported via Gettext! Liberator already finds the best language for each request, now it also sets the locale using Gettext.put_locale/1. Just call gettext directly in your Resources. (docs) (#8, #10)

Debugging and tracing upgrades

  • Tracing individual step duration is now available. Access the :duration key for each decision in the tracing list to get that decision's duration, in native time units.
  • You can now generate a Graphviz file for either the default decision tree or your own resource's decision tree using the mix liberator.chart mix task.
  • Telemetry is now sent upon each request. Three events [:liberator, :request, :start], [:liberator, :request, :stop], and [:liberator, :request, :exception] are sent. See the docs for Liberator.Resource for more information.

Docs for debugging and tracing

Changed

  • The Vary header is now served by default, with a value of Accept-Encoding and Accept-Language.
  • Now serves the location header if you've returned a map with the :location key, or assigned it on the conn.
  • Some decision functions were rearranged.
  • Raised errors are now wrapped in custom exceptions. They're the same errors with the same messages, just with different wrapper types.
  • Entries in the trace list have changed from tuples to maps in order to support more tracing information. The step is now a map member named :step, and the value is :value

Deprecated

  • The malformed?/1 is now deprecated, use well_formed?/1 instead. This lets that decision function return data, and it's the ideal place for parsing the body. (#15)

Fixed

  • The etag header is now included in the response, if you have overridden it. (#17)
  • Now serves the last-modified header based on the return value from the last_modified/1 callback. (#18)
  • Non-printable-String return values from handlers will now be passed through inspect/1 when the content type is text/plain. Printable strings will be passed through without hassle. (#7)
  • All responses now include an allow header, fixing the cases where one was required but not provided, like in an OPTIONS request, and when returning a 405 Method Not Allowed. (#9, #12)