-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Follow the phoenix recommandations to handle the json rendering, add …
…fallback actions and return the errors by following the RFC 7807.
- Loading branch information
Burgy Benjamin
committed
Dec 18, 2023
1 parent
240b21e
commit d81ac60
Showing
13 changed files
with
158 additions
and
143 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[ | ||
line_length: 300, | ||
line_length: 150, | ||
import_deps: [:phoenix], | ||
inputs: ["*.{ex,exs}", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule KsuiteMiddlewareWeb.CalendarJSON do | ||
alias KsuiteMiddlewareWeb.Models.KsuiteCalendarEvent | ||
|
||
def events(%{events: [%KsuiteCalendarEvent{} | _] = events}), do: events | ||
|
||
def render("400.json", %{reason: reason}), | ||
do: %{title: "invalid argument", status: 400, detail: reason} | ||
|
||
def render("404.json", %{reason: reason}), | ||
do: %{ | ||
title: "not found", | ||
status: 404, | ||
detail: reason, | ||
description: | ||
"Oh snap! It seems we've hit a hiccup in our treasure hunt—what you seek is playing hide and seek in the " <> | ||
"digital jungle! Keep those eagle eyes peeled, and perhaps try a different map or a clever keyword dance " <> | ||
"to coax the elusive information out of hiding." | ||
} | ||
|
||
def render("500.json", %{reason: reason}), | ||
do: %{title: "internal server error", status: 500, detail: reason} | ||
|
||
def render(template, _assigns), | ||
do: %{detail: Phoenix.Controller.status_message_from_template(template)} | ||
end |
This file was deleted.
Oops, something went wrong.
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
16 changes: 16 additions & 0 deletions
16
lib/ksuite_middleware_web/controllers/fallback_controller.ex
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule KsuiteMiddlewareWeb.FallbackController do | ||
@moduledoc """ | ||
Translates controller action results into valid `Plug.Conn` responses. | ||
See `Phoenix.Controller.action_fallback/1` for more details. | ||
""" | ||
use KsuiteMiddlewareWeb, :controller | ||
|
||
# This clause is an example of how to handle resources that cannot be found. | ||
def call(conn, {:error, :not_found}) do | ||
conn | ||
|> put_status(:not_found) | ||
|> put_view(json: KsuiteMiddlewareWeb.ErrorJSON) | ||
|> render(:"404") | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule KsuiteMiddlewareWeb.KdriveJSON do | ||
alias KsuiteMiddleware.State | ||
|
||
def render("400.json", %{reason: :invalid_integer}), | ||
do: %{title: "invalid argument", status: 400, detail: "The given file_id was invalid."} | ||
|
||
def render("400.json", %{reason: :missing_file_id}), | ||
do: %{title: "missing argument", status: 400, detail: "The file_id was missing."} | ||
|
||
def render("400.json", %{reason: :bad_api_key}), | ||
do: %{ | ||
title: "bad api key", | ||
status: 400, | ||
detail: "The API key is invalid, please verify the 'KSUITE_API_TOKEN'.", | ||
description: | ||
"Whoopsie-daisy! Looks like we got a little lost in API land. Our secret handshake seems a bit off. " <> | ||
"Double-check that magic spell in the enchanted scroll labeled `KSUITE_API_TOKEN` and make sure it's been " <> | ||
"properly whispered into the winds of the environment variable realm." | ||
} | ||
|
||
def render("502.json", %{reason: :invalid_response_from_api}), | ||
do: %{title: "bad gateway", status: 502, detail: "The upstream api returned an invalid response.", server: State.get_ksuite_api_server()} | ||
|
||
def render("500.json", %{reason: :unknown}), | ||
do: %{title: "internal server error", status: 500, detail: "An unhandled error was returned, please contact the administrator."} | ||
|
||
def render(template, _assigns), | ||
do: %{detail: Phoenix.Controller.status_message_from_template(template)} | ||
end |
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.