1 module hunt.framework.http.HttpErrorResponseHandler; 2 3 import hunt.http.HttpHeader; 4 import hunt.http.HttpStatus; 5 import hunt.http.routing.handler.ErrorResponseHandler; 6 import hunt.http.routing.RoutingContext; 7 8 import std.range; 9 10 11 /** 12 * 13 */ 14 class HttpErrorResponseHandler : ErrorResponseHandler { 15 16 override void render(RoutingContext context, int status, Exception t) { 17 18 HttpStatusCode code = HttpStatus.getCode(status); 19 if(code == HttpStatusCode.Null) 20 code = HttpStatusCode.INTERNAL_SERVER_ERROR; 21 22 context.setStatus(status); 23 context.getResponseHeaders().put(HttpHeader.CONTENT_TYPE, "text/html"); 24 context.end(errorPageHtml(status)); 25 } 26 27 } 28 29 30 31 // dfmt off 32 string errorPageHtml(int code , string _body = "") 33 { 34 import std.conv : to; 35 36 string text = code.to!string; 37 text ~= " "; 38 text ~= HttpStatus.getMessage(code); 39 40 string html = `<!doctype html> 41 <html lang="en"> 42 <meta charset="utf-8"> 43 <title>`; 44 45 html ~= text; 46 html ~= `</title> 47 <meta name="viewport" content="width=device-width, initial-scale=1"> 48 <style> 49 50 * { 51 line-height: 3; 52 margin: 0; 53 } 54 55 html { 56 color: #888; 57 display: table; 58 font-family: sans-serif; 59 height: 100%; 60 text-align: center; 61 width: 100%; 62 } 63 64 body { 65 display: table-cell; 66 vertical-align: middle; 67 margin: 2em auto; 68 } 69 70 h1 { 71 color: #555; 72 font-size: 2em; 73 font-weight: 400; 74 } 75 76 p { 77 margin: 0 auto; 78 width: 90%; 79 } 80 81 </style> 82 </head> 83 <body> 84 <h1>`; 85 html ~= text; 86 html ~= `</h1> 87 <p>Sorry!! Unable to complete your request :(</p> 88 `; 89 if(_body.length > 0) 90 html ~= "<p>" ~ _body ~ "</p>"; 91 html ~= ` 92 </body> 93 </html> 94 `; 95 96 return html; 97 } 98 99 string errorPageWithStack(int code , string _body = "") 100 { 101 import std.conv : to; 102 103 string text = code.to!string; 104 text ~= " "; 105 text ~= HttpStatus.getMessage(code); 106 107 string html = `<!doctype html> 108 <html lang="en"> 109 <meta charset="utf-8"> 110 <title>`; 111 112 html ~= text; 113 html ~= `</title> 114 <meta name="viewport" content="width=device-width, initial-scale=1"> 115 116 </head> 117 <body> 118 <h1>`; 119 html ~= text; 120 html ~= `</h1> 121 <p>Sorry!! Unable to complete your request :(</p> 122 `; 123 124 125 if(!_body.empty) { 126 html ~= _body; 127 } 128 129 html ~= ` 130 </body> 131 </html> 132 `; 133 134 return html; 135 } 136 // dfmt on