Giter Site home page Giter Site logo

esvinson / explode Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pkinney/explode

0.0 0.0 0.0 161 KB

An easy utility for responding with standard HTTP/JSON error payloads in Plug- and Phoenix-based applications

License: MIT License

Elixir 100.00%

explode's Introduction

Explode

Build Status Hex.pm

An easy utility for responding with standard HTTP/JSON error payloads in Plug- and Phoenix-based applications.

This project is heavily influenced by Hapi's Boom module [https://github.com/hapijs/boom]

Installation

defp deps do
  [{:explode, "~> 1.0.0"}]
end

Usage

Turns

conn
|> put_resp_content_type("application/json")
|> send_resp(:unauthorized, "{\"statusCode\":403,\"error\":\"Forbidden\",\"message\":\"You are not authorized to view this resource\"}")
|> halt

into

conn |> Explode.with(403, "You are not authorized to view this resource")

# or

conn |> Explode.forbidden("You are not authorized to view this resource")

Error Responses

Explode sets the status code of the response and normalizes errors into a single structure:

{
  "statusCode": 403,
  "error": "Not Authorized",
  "message": "You are not authorized to view this resource"
}

JSON API

In order to be compliant with the JSON API spec (http://jsonapi.org/format/#errors), if the request headers in the conn object passed to Explode includes Accept of "application/vnd.api+json", then the error response will be formatted to match the JSON API Error Object spect (http://jsonapi.org/format/#errors). Additionally, the Content-Type header of the response will also be set to "application/vnd.api+json".

{
  "errors": [
    {
      "status": 403,
      "title": "Forbidden",
      "detail": "You are not authorized to view this resource"
    }
  ]
}

Ecto Changeset

Explode will also accept an Ecto.Changeset struct instead of a message. This allows a Pheonix application to directly hand a Changeset to Explode without having to do an traversal of errors.

changeset = %Ecto.Changeset{
  action: :insert,
  types: %{},
  changes: %{first_name: "John", last_name: "Smith", password: "foo"},
  errors: [
    password: {"should be at least %{count} character(s)", [count: 5, validation: :length, min: 5]},
    email: {"can't be blank", [validation: :required]}],
  valid?: false,
  data: %User{}
}

Explode.with(conn, changeset)

will result in the following error response:

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "`email` can't be blank, `password` should be at least 5 character(s)"
}

Bring your own JSON encoder

Explode by default with use Poison as the JSON encoding library. If you want to change that out, you can do so in config.exs

config(:explode, :json_library, Jason)

explode's People

Contributors

pkinney avatar gaker avatar zbarnes757 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.