Last active
March 17, 2021 09:10
-
-
Save addrummond/49d508d2fefdb4548e1e41367c8214ef to your computer and use it in GitHub Desktop.
Example HTML lint plug code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule HtmlLintPlug do | |
require Logger | |
def init(opts), do: opts | |
def call(conn, _opts) do | |
Plug.Conn.register_before_send(conn, &lint/1) | |
end | |
defmodule Error do | |
defexception message: "HTML linter returned errors" | |
end | |
defp lint(conn) do | |
body_string = to_string(conn.resp_body) | |
result = my_html_linting_function(body_string) | |
case result do | |
:ok -> | |
conn | |
{:error, error} -> | |
case Mix.env() do | |
:dev -> | |
Logger.warn("#{inspect(error)}") | |
conn | |
:test -> | |
raise Error, message: inspect(error) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment