Skip to content

Commit

Permalink
Remove early exception handling from file utils
Browse files Browse the repository at this point in the history
It's better to handle exceptions on the server level in order to return
the proper http status code
  • Loading branch information
psyomn committed Mar 24, 2017
1 parent 73462c8 commit 1046609
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 17 deletions.
5 changes: 0 additions & 5 deletions src/file_utils.adb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,5 @@ package body File_Utils is

IO.Close (File => The_File);
return Ada.Strings.Unbounded.To_String (Contents);
exception when others =>
-- TODO: Probably 404 here
IO.Put_Line ("Warning: request to a non existant file was made.");
IO.Put_Line (">> Path: " & File_Name);
return "Error";
end Read;
end File_Utils;
56 changes: 56 additions & 0 deletions src/http_status.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package body HTTP_Status is
function Message_Of_Code (C : Code) return String is
begin
case C is
when CONTINUE => return "Continue";
when SWITCHING_PROTOCOLS => return "Switching Protocols";

when OK => return "Ok";
when CREATED => return "Created";
when ACCEPTED => return "Accepted";
when NON_AUTHORITATIVE_INFORMATION =>
return "Non Authoritative Information";
when NO_CONTENT => return "No Content";
when RESET_CONTENT => return "Reset Content";
when PARTIAL_CONTENT => return "Partial Content";

when MULTIPLE_CHOICES => return "Multiple Choices";
when MOVED_PERMANENTLY => return "Moved Permanently";
when FOUND => return "Found";
when SEE_OTHER => return "See Other";
when NOT_MODIFIED => return "Not Modified";
when USE_PROXY => return "Use Proxy";
when UNUSED => return "Unused";
when TEMPORARY_REDIRECT => return "Temporary Redirect";

when BAD_REQUEST => return "Bad Request";
when UNAUTHORIZED => return "Unauthorized";
when PAYMENT_REQUIRED => return "Payment Required";
when FORBIDDEN => return "Forbidden";
when NOT_FOUND => return "Not Found";
when METHOD_NOT_ALLOWED => return "Method Not Allowed";
when NOT_ACCEPTABLE => return "Not Acceptable";
when PROXY_AUTH_REQUIRED => return "Proxy Auth Required";
when REQUEST_TIMEOUT => return "Request Timeout";
when CONFLICT => return "Conflict";
when GONE => return "Gone";
when LENGTH_REQUIRED => return "Length Required";
when PRECONDITION_FAILED => return "Precondition Failed";
when REQUEST_ENTITY_TOO_LARGE => return "Request Entity Too Large";
when REQUEST_URI_TOO_LONG => return "Request Uri Too Long";
when UNSUPPORTED_MEDIA_TYPE => return "Unsupported Media Type";
when REQUESTED_RANGE_NOT_SATISFIABLE =>
return "Requested Range Not Satisfiable";
when EXPECTATION_FAILED => return "Expectation Failed";

when INTERNAL_ERROR => return "Internal Error";
when NOT_IMPLEMENTED => return "Not Implemented";
when BAD_GATEWAY => return "Bad Gateway";
when SERVICE_UNAVAILABLE => return "Service Unavailable";
when GATEWAY_TIMEOUT => return "Gateway Timeout";
when HTTP_VERSION_NOT_SUPPORTED =>
return "Http Version Not Supported";
when others => raise Bad_Code_Error;
end case;
end Message_Of_Code;
end HTTP_Status;
71 changes: 59 additions & 12 deletions src/http_status.ads
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
package Status_Codes is
OK : constant := 200;
CREATED : constant := 201;
ACCEPTED : constant := 202;

BAD_REQUEST : constant := 400;
UNAUTHORIZED : constant := 401;
FORBIDDEN : constant := 403;
NOT_FOUND : constant := 404;

INTERNAL_ERROR : constant := 500;
end Status_Codes;
package HTTP_Status is
type Code is new Positive;
type Code_Range is range 100 .. 599;

Bad_Code_Error : exception;

-- Info
CONTINUE : constant Code := 100;
SWITCHING_PROTOCOLS : constant Code := 101;

-- Success
OK : constant Code := 200;
CREATED : constant Code := 201;
ACCEPTED : constant Code := 202;
NON_AUTHORITATIVE_INFORMATION : constant Code := 203;
NO_CONTENT : constant Code := 204;
RESET_CONTENT : constant Code := 205;
PARTIAL_CONTENT : constant Code := 206;

-- Redirections
MULTIPLE_CHOICES : constant Code := 300;
MOVED_PERMANENTLY : constant Code := 301;
FOUND : constant Code := 302;
SEE_OTHER : constant Code := 303;
NOT_MODIFIED : constant Code := 304;
USE_PROXY : constant Code := 305;
UNUSED : constant Code := 306;
TEMPORARY_REDIRECT : constant Code := 307;

-- PEBKAC
BAD_REQUEST : constant Code := 400;
UNAUTHORIZED : constant Code := 401;
PAYMENT_REQUIRED : constant Code := 402;
FORBIDDEN : constant Code := 403;
NOT_FOUND : constant Code := 404;
METHOD_NOT_ALLOWED : constant Code := 405;
NOT_ACCEPTABLE : constant Code := 406;
PROXY_AUTH_REQUIRED : constant Code := 407;
REQUEST_TIMEOUT : constant Code := 408;
CONFLICT : constant Code := 409;
GONE : constant Code := 410;
LENGTH_REQUIRED : constant Code := 411;
PRECONDITION_FAILED : constant Code := 412;
REQUEST_ENTITY_TOO_LARGE : constant Code := 413;
REQUEST_URI_TOO_LONG : constant Code := 414;
UNSUPPORTED_MEDIA_TYPE : constant Code := 415;
REQUESTED_RANGE_NOT_SATISFIABLE : constant Code := 416;
EXPECTATION_FAILED : constant Code := 417;

-- BOOM
INTERNAL_ERROR : constant Code := 500;
NOT_IMPLEMENTED : constant Code := 501;
BAD_GATEWAY : constant Code := 502;
SERVICE_UNAVAILABLE : constant Code := 503;
GATEWAY_TIMEOUT : constant Code := 504;
HTTP_VERSION_NOT_SUPPORTED : constant Code := 505;

function Message_Of_Code (C : Code) return String;
end HTTP_Status;

0 comments on commit 1046609

Please sign in to comment.