From: Michal MalĂ˝ Date: Sun, 30 Nov 2014 19:32:12 +0000 (+0100) Subject: - Add static error pages to display when template generation fails X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=b3270be50bf3053c290784ed1f90a4210d714baf;p=Nine-Q.git - Add static error pages to display when template generation fails - Redirect to index only when problem_category GET parameters is empty --- diff --git a/bin/styles/main.css b/bin/styles/main.css index 2c0ffb7..889d0fd 100644 --- a/bin/styles/main.css +++ b/bin/styles/main.css @@ -50,6 +50,13 @@ font-style: italic; } +.error_text { + font-size: 12pt; + margin-left: 32px; + margin-right: 32px; + margin-bottom: 16px; +} + .exponent { font-size: 75%; vertical-align: super; @@ -117,6 +124,16 @@ a.main_navlink { -webkit-border-radius: 4px; } +a.default_link { + color: inherit; + font-weight: bold; + text-decoration: none; +} + +a:visited.default_link { + color: black; +} + a:hover.main_navlink { background-color: #58acf4; } @@ -137,6 +154,21 @@ a:visited.footer_link { text-decoration: none; } +div.error_box { + background-color: #ff4141; + margin-left: 32px; + margin-right: 32px; + padding-top: 16px; + padding-bottom: 16px; + + /* CSS3 only */ + border-radius: 10px; + /* Pre CSS3 Mozilla */ + -moz-border-radius: 10px; + /* Pre CSS3 Webkit */ + -webkit-border-radius: 10px; +} + div.hint_block { margin-left: 32px; margin-right: 32px; diff --git a/bin/templates/error.html b/bin/templates/error.html new file mode 100644 index 0000000..eac1f32 --- /dev/null +++ b/bin/templates/error.html @@ -0,0 +1,5 @@ +
+
Something didn't go quite as it was supposed to. We're sorry about this.
+
@_ERROR_MESSAGE_@
+
You can report the problem to the webmaster.
+
diff --git a/bin/templates/static/error_display_answer.html b/bin/templates/static/error_display_answer.html new file mode 100644 index 0000000..e97b127 --- /dev/null +++ b/bin/templates/static/error_display_answer.html @@ -0,0 +1,35 @@ + + + + + + + @_META_EXPIRE_NOW_@ + @_META_NO_CACHE_@ + + TGen_Prototype + + + +
+
+ +
+
+ +
+
Something didn't go quite as it was supposed to. We're sorry about this.
+
Cannot display answer.
+
You can report the problem to the webmaster.
+
+ +
+
+ +
+ + + diff --git a/bin/templates/static/error_display_assignment.html b/bin/templates/static/error_display_assignment.html new file mode 100644 index 0000000..21365a4 --- /dev/null +++ b/bin/templates/static/error_display_assignment.html @@ -0,0 +1,35 @@ + + + + + + + @_META_EXPIRE_NOW_@ + @_META_NO_CACHE_@ + + TGen_Prototype + + + +
+
+ +
+
+ +
+
Something didn't go quite as it was supposed to. We're sorry about this.
+
Cannot display assignment.
+
You can report the problem to the webmaster.
+
+ +
+
+ +
+ + + diff --git a/bin/templates/static/error_prepare_problem.html b/bin/templates/static/error_prepare_problem.html new file mode 100644 index 0000000..1244e32 --- /dev/null +++ b/bin/templates/static/error_prepare_problem.html @@ -0,0 +1,35 @@ + + + + + + + @_META_EXPIRE_NOW_@ + @_META_NO_CACHE_@ + + TGen_Prototype + + + +
+
+ +
+
+ +
+
Something didn't go quite as it was supposed to. We're sorry about this.
+
Cannot prepare problem.
+
You can report the problem to the webmaster.
+
+ +
+
+ +
+ + + diff --git a/bin/templates/static/error_problem_category.html b/bin/templates/static/error_problem_category.html new file mode 100644 index 0000000..2210513 --- /dev/null +++ b/bin/templates/static/error_problem_category.html @@ -0,0 +1,35 @@ + + + + + + + @_META_EXPIRE_NOW_@ + @_META_NO_CACHE_@ + + TGen_Prototype + + + +
+
+ +
+
+ +
+
Something didn't go quite as it was supposed to. We're sorry about this.
+
Problem category is missing.
+
You can report the problem to the webmaster.
+
+ +
+
+ +
+ + + diff --git a/bin/templates/static/error_problem_id.html b/bin/templates/static/error_problem_id.html new file mode 100644 index 0000000..fdba946 --- /dev/null +++ b/bin/templates/static/error_problem_id.html @@ -0,0 +1,35 @@ + + + + + + + @_META_EXPIRE_NOW_@ + @_META_NO_CACHE_@ + + TGen_Prototype + + + +
+
+ +
+
+ +
+
Something didn't go quite as it was supposed to. We're sorry about this.
+
Problem ID not present.
+
You can report the problem to the webmaster.
+
+ +
+
+ +
+ + + diff --git a/bin/templates/static/error_uid_registration.html b/bin/templates/static/error_uid_registration.html new file mode 100644 index 0000000..3431246 --- /dev/null +++ b/bin/templates/static/error_uid_registration.html @@ -0,0 +1,35 @@ + + + + + + + @_META_EXPIRE_NOW_@ + @_META_NO_CACHE_@ + + TGen_Prototype + + + +
+
+ +
+
+ +
+
Something didn't go quite as it was supposed to. We're sorry about this.
+
Cannot register UID.
+
You can report the problem to the webmaster.
+
+ +
+
+ +
+ + + diff --git a/src/face_generators/face_generator.adb b/src/face_generators/face_generator.adb index f95104b..41f47c8 100644 --- a/src/face_generators/face_generator.adb +++ b/src/face_generators/face_generator.adb @@ -2,6 +2,26 @@ with Ada.Strings.Unbounded; package body Face_Generator is + function Generate_Error_Face(HTML: out HTML_Code; Message: in String) return RetCode is + use AWS.Templates; + + Temp: HTML_Code; + Translations_Hdr: Translate_Set; + Translations: Translate_Set; + begin + Insert(Translations_Hdr, Assoc(HEADER_CAPTION_KEY, "Oops...")); + HTML := Parse(Filename => "templates/header.html", Translations => Translations_Hdr); + + Insert(Translations, Assoc(ERROR_MESSAGE_KEY, Message)); + Temp := Parse(Filename => "templates/error.html", Translations => Translations); + Append_HTML(Source => HTML, New_Item => Temp); + + Temp := Parse(Filename => "templates/footer.html"); + Append_HTML(Source => HTML, New_Item => Temp); + + return OK; + end Generate_Error_Face; + function Generate_Index_Face(HTML: out HTML_Code) return RetCode is use AWS.Templates; diff --git a/src/face_generators/face_generator.ads b/src/face_generators/face_generator.ads index 894a392..5e546d0 100644 --- a/src/face_generators/face_generator.ads +++ b/src/face_generators/face_generator.ads @@ -4,8 +4,8 @@ with AWS.Templates; use Global_Types; package Face_Generator is + function Generate_Error_Face(HTML: out HTML_Code; Message: in String) return RetCode; function Generate_Index_Face(HTML: out HTML_Code) return RetCode; - function Generate_Face(Assignment: in Problem_Generator_Syswides.Assignment_Info.Map; Parameters: in Problem_Generator_Syswides.Parameters_Info.Map; HTML: out HTML_Code; @@ -36,6 +36,7 @@ private HTML: out HTML_Code; Pr_ID: in String; Pr_Cat: in String) return RetCode; + ERROR_MESSAGE_KEY: constant String := "ERROR_MESSAGE"; HEADER_CAPTION_KEY: constant String := "HEADER_CAPTION"; HINTS_SECTION_KEY: constant String := "HINTS_SECTION"; META_EXPIRE_NOW_KEY: constant String := "META_EXPIRE_NOW"; diff --git a/src/handlers/handler_check_answer.adb b/src/handlers/handler_check_answer.adb index 95b22f8..bfd9ab0 100644 --- a/src/handlers/handler_check_answer.adb +++ b/src/handlers/handler_check_answer.adb @@ -6,12 +6,11 @@ with AWS.Response; with AWS.Session; with AWS.Status; +with Face_Generator_Static; with Global_Types; with Problem_Manager; with Problem_Generator_Syswides; -with Ada.Text_IO; - use Global_Types; package body Handler_Check_Answer is @@ -38,9 +37,7 @@ package body Handler_Check_Answer is -- Get UID Success := Problem_Manager.Get_UID(Raw_UID, UID); if Success = False then - -- UID could not have been registered - -- TODO: Print some sensible error message, for now just redirect to index - Ada.Text_IO.Put_Line("UID has not been registered: " & Raw_UID); + -- This UID is invalid, redirect to index return AWS.Response.URL(Location => "/"); end if; @@ -57,22 +54,25 @@ package body Handler_Check_Answer is -- Get problem ID if Answer.Find(Problem_Generator_Syswides.RESERVED_PROBLEM_ID_KEY) = Answer_Info.No_Element then - Ada.Text_IO.Put_Line("Problem ID not found!"); - return AWS.Response.URL(Location => "/"); + return AWS.Response.Build(Content_Type => AWS.MIME.Text_HTML, + Message_Body => HTML_To_Fixed_String(Face_Generator_Static.Error_Problem_ID), + Status_Code => AWS.Messages.S200); end if; begin Pr_ID := Problem_ID'Value(Answer.Element(Problem_Generator_Syswides.RESERVED_PROBLEM_ID_KEY)); exception when Constraint_Error => - Ada.Text_IO.Put_Line("Invalid problem ID value"); - return AWS.Response.URL(Location => "/"); + return AWS.Response.Build(Content_Type => AWS.MIME.Text_HTML, + Message_Body => HTML_To_Fixed_String(Face_Generator_Static.Error_Problem_ID), + Status_Code => AWS.Messages.S200); end; Ret := Problem_Manager.Display_Checked_Answer(UID, Answer, HTML, Pr_ID); if Ret /= OK then - Ada.Text_IO.Put_Line("No such problem in storage"); - return AWS.Response.URL(Location => "/"); + return AWS.Response.Build(Content_Type => AWS.MIME.Text_HTML, + Message_Body => HTML_To_Fixed_String(Face_Generator_Static.Error_Display_Answer), + Status_Code => AWS.Messages.S200); end if; return AWS.Response.Build(Content_Type => AWS.MIME.Text_HTML, @@ -80,7 +80,6 @@ package body Handler_Check_Answer is Status_Code => AWS.Messages.S200); end; when others => - Ada.Text_IO.Put_Line("Invalid request"); return AWS.Response.URL(Location => "/"); end case; end Handle; diff --git a/src/handlers/handler_next_problem.adb b/src/handlers/handler_next_problem.adb index 1ddbd19..aa7407f 100644 --- a/src/handlers/handler_next_problem.adb +++ b/src/handlers/handler_next_problem.adb @@ -8,6 +8,7 @@ with AWS.Status; with Ada.Text_IO; +with Face_Generator_Static; with Global_Types; with Problem_Generator_Syswides; with Problem_Manager; @@ -38,8 +39,7 @@ package body Handler_Next_Problem is -- Get UID Success := Problem_Manager.Get_UID(Raw_UID, UID); if Success = False then - -- UID could not have been registered - -- TODO: Print some sensible error message, for now just redirect to index + -- This UID is invalid, redirect to index return AWS.Response.URL(Location => "/"); end if; @@ -57,8 +57,9 @@ package body Handler_Next_Problem is -- Create a new problem if Problem_Parameters.Find(Problem_Generator_Syswides.RESERVED_PROBLEM_CATEGORY_KEY) = Parameters_Info.No_Element then - Ada.Text_IO.Put_Line("No problem category tag"); - return AWS.Response.URL(Location => "/"); + return AWS.Response.Build(Content_Type => AWS.MIME.Text_HTML, + Message_Body => HTML_To_Fixed_String(Face_Generator_Static.Error_Problem_Category), + Status_Code => AWS.Messages.S200); end if; declare @@ -66,14 +67,16 @@ package body Handler_Next_Problem is begin Ret := Problem_Manager.Prepare_Problem(UID, Raw_P_Cat, Problem_Parameters); if Ret /= OK then - -- TODO: Handle error in a less reckless manner - return AWS.Response.URL(Location => "/"); + return AWS.Response.Build(Content_Type => AWS.MIME.Text_HTML, + Message_Body => HTML_To_Fixed_String(Face_Generator_Static.Error_Prepare_Problem), + Status_Code => AWS.Messages.S200); end if; -- Display new problem Ret := Problem_Manager.Display_Assignment(UID, HTML); if Ret /= OK then - -- TODO: Handle error in a less reckless manner - return AWS.Response.URL(Location => "/"); + return AWS.Response.Build(Content_Type => AWS.MIME.Text_HTML, + Message_Body => HTML_To_Fixed_String(Face_Generator_Static.Error_Display_Assignment), + Status_Code => AWS.Messages.S200); end if; return AWS.Response.Build(Content_Type => AWS.MIME.Text_HTML, diff --git a/src/handlers/handler_start.adb b/src/handlers/handler_start.adb index 6cbea1b..2e61405 100644 --- a/src/handlers/handler_start.adb +++ b/src/handlers/handler_start.adb @@ -3,11 +3,10 @@ with AWS.MIME; with AWS.Response; with AWS.Session; with AWS.Status; +with Face_Generator_Static; with Global_Types; with Problem_Manager; ---with Ada.Text_IO; - use Global_Types; package body Handler_Start is @@ -26,7 +25,12 @@ package body Handler_Start is UID: Unique_ID; Ret: RetCode; Success: Boolean; - begin + begin + -- Check that the problem category is not empty + if Raw_Problem_Category'Length = 0 then + return AWS.Response.URL(Location => "/"); + end if; + -- Register new UID if necessary and create a first problem Success := Problem_Manager.Get_UID(Raw_UID, UID); if Success = False then @@ -35,7 +39,9 @@ package body Handler_Start is if Ret /= OK then -- UID could not have been registered -- TODO: Print some sensible error message, for now just redirect to index - return AWS.Response.URL(Location => "/"); + return AWS.Response.Build(Content_Type => AWS.MIME.Text_HTML, + Message_Body => HTML_To_Fixed_String(Face_Generator_Static.Error_UID_Registration), + Status_Code => AWS.Messages.S200); end if; -- Save the new UID AWS.Session.Set(SID, "UID", Unique_ID'Image(UID)); @@ -47,12 +53,16 @@ package body Handler_Start is if Ret /= OK then -- Something went wrong when generating the problem -- TODO: Print some sensible error message, for now just redirect to index - return AWS.Response.URL(Location => "/"); + return AWS.Response.Build(Content_Type => AWS.MIME.Text_HTML, + Message_Body => HTML_To_Fixed_String(Face_Generator_Static.Error_Prepare_Problem), + Status_Code => AWS.Messages.S200); end if; Ret := Problem_Manager.Display_Assignment(UID, HTML); if Ret /= OK then - HTML := To_HTML_Code("Cannot display assignment"); + return AWS.Response.Build(Content_Type => AWS.MIME.Text_HTML, + Message_Body => HTML_To_Fixed_String(Face_Generator_Static.Error_Display_Assignment), + Status_Code => AWS.Messages.S200); end if; return AWS.Response.Build(Content_Type => AWS.MIME.Text_HTML, Message_Body => HTML_To_Fixed_String(HTML), diff --git a/src/problem_manager.adb b/src/problem_manager.adb index d4f6f1f..7457b7a 100644 --- a/src/problem_manager.adb +++ b/src/problem_manager.adb @@ -21,17 +21,18 @@ package body Problem_Manager is Ret := Stored.Problem.Get_Parameters(Parameters); if Ret /= OK then Stored.Mutex.Unlock; - return Ret; + return Face_Generator.Generate_Error_Face(HTML, ERRMSG_GET_PARAMETERS & " (" & RetCode'Image(Ret) & ")"); end if; begin Ret := Stored.Problem.Get_Assignment(Assignment); if Ret /= OK then Stored.Mutex.Unlock; - return Ret; + return Face_Generator.Generate_Error_Face(HTML, ERRMSG_GET_ASSIGNMENT & " (" & RetCode'Image(Ret) & ")"); end if; exception when others => + return Face_Generator.Generate_Error_Face(HTML, ERRMSG_UNHANDLED_EXCEPTION); Stored.Mutex.Unlock; return E_UNKW; end; @@ -60,7 +61,7 @@ package body Problem_Manager is Ret := Stored.Problem.Get_Parameters(Parameters); if Ret /= OK then Stored.Mutex.Unlock; - return Ret; + return Face_Generator.Generate_Error_Face(HTML, ERRMSG_GET_PARAMETERS & " (" & RetCode'Image(Ret) & ")"); end if; -- Get assignment @@ -68,12 +69,12 @@ package body Problem_Manager is Ret := Stored.Problem.Get_Assignment(Assignment); if Ret /= OK then Stored.Mutex.Unlock; - return Ret; + return Face_Generator.Generate_Error_Face(HTML, ERRMSG_GET_ASSIGNMENT & " (" & RetCode'Image(Ret) & ")"); end if; exception when others => Stored.Mutex.Unlock; - return E_UNKW; + return Face_Generator.Generate_Error_Face(HTML, ERRMSG_UNHANDLED_EXCEPTION); end; Pr_Cat := Stored.Category; diff --git a/src/problem_manager.ads b/src/problem_manager.ads index 5fccf1e..ab460e9 100644 --- a/src/problem_manager.ads +++ b/src/problem_manager.ads @@ -43,6 +43,10 @@ private end record; type User_Session_All_Access is access all User_Session_Data; + ERRMSG_GET_ASSIGNMENT: constant String := "Unable to get assignment"; + ERRMSG_GET_PARAMETERS: constant String := "Unable to get problem parameters"; + ERRMSG_UNHANDLED_EXCEPTION: constant String := "Unhandled exception occured"; + package Session_Keeping is new Ada.Containers.Ordered_Maps(Key_Type => Unique_ID, Element_Type => User_Session_All_Access); protected Active_Sessions is