From 708dc012953f555e0434b366cb004977d0821979 Mon Sep 17 00:00:00 2001 From: neodarz Date: Fri, 17 Feb 2017 11:33:27 +0100 Subject: Adding error handling --- web/controllers/audio_file_api_controller.ex | 18 +++++++++++++----- web/views/error_view.ex | 11 +++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) (limited to 'web') diff --git a/web/controllers/audio_file_api_controller.ex b/web/controllers/audio_file_api_controller.ex index 11ae1a7..afc23ac 100644 --- a/web/controllers/audio_file_api_controller.ex +++ b/web/controllers/audio_file_api_controller.ex @@ -51,12 +51,20 @@ defmodule TheTranscriberBackend.AudioFileAPIController do end def delete(conn, %{"id" => id}) do + path = "/media/phoenix_test/" audio_file_api = Repo.get!(AudioFileAPI, id) + cond do + audio_file_api !=nil -> + # Here we use delete! (with a bang) because we expect + # it to always work (and if it does not, it will raise). + File.rm("#{path}#{audio_file_api.id}_#{audio_file_api.audio_path}") + Repo.delete!(audio_file_api) - # Here we use delete! (with a bang) because we expect - # it to always work (and if it does not, it will raise). - Repo.delete!(audio_file_api) - - send_resp(conn, :no_content, "") + conn + |> send_resp(200, "Audio file deleted successfully.") + audio_file_api == nil -> + conn + |> send_resp(404, "File not found in database !") + end end end diff --git a/web/views/error_view.ex b/web/views/error_view.ex index e47ebee..5906829 100644 --- a/web/views/error_view.ex +++ b/web/views/error_view.ex @@ -14,4 +14,15 @@ defmodule TheTranscriberBackend.ErrorView do def template_not_found(_template, assigns) do render "500.html", assigns end + + def render("404.json", %{reason: reason}) do + message = case reason do + %Phoenix.Router.NoRouteError{} -> "Route not found" + %Ecto.NoResultsError{} -> "File not found in database !" + _ -> "Uncaught exception" + end + # ContactService.ResponseHelper.error(message) + %{error: message} +end + end -- cgit v1.2.1