1 /** 2 * Copyright © DiamondMVC 2019 3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE) 4 * Author: Jacob Jensen (bausshf) 5 */ 6 module diamond.errors.errorhandler; 7 8 import diamond.core.apptype; 9 10 static if (isWeb) 11 { 12 import vibe.d : HTTPServerRequest, HTTPServerResponse, HTTPServerErrorInfo, 13 HTTPStatus, HTTPStatusException; 14 15 import diamond.core.io; 16 import diamond.core.webconfig; 17 import diamond.core.websettings; 18 19 /** 20 * Handles user exceptions. 21 * Params: 22 * e = The exception, if any. 23 * request = The request. 24 * response = The response. 25 * error = The error information, if any. 26 */ 27 void handleUserException(Exception e, HTTPServerRequest request, 28 HTTPServerResponse response, HTTPServerErrorInfo error) 29 { 30 try 31 { 32 response.statusCode = (error ? error.code : 500); 33 34 auto httpStatusException = cast(HTTPStatusException)e; 35 36 if (httpStatusException) 37 { 38 response.statusCode = httpStatusException.status; 39 40 if (httpStatusException.status == 404) 41 { 42 foreach (headerKey,headerValue; webConfig.defaultHeaders.notFound) 43 { 44 response.headers[headerKey] = headerValue; 45 } 46 47 if (webSettings) 48 { 49 webSettings.onNotFound(request,response); 50 } 51 else 52 { 53 response.bodyWriter.write("Not found ..."); 54 } 55 56 return; 57 } 58 } 59 60 foreach (headerKey,headerValue; webConfig.defaultHeaders.error) 61 { 62 response.headers[headerKey] = headerValue; 63 } 64 65 if (webSettings) 66 { 67 webSettings.onHttpError(e,request,response,error); 68 } 69 else 70 { 71 response.bodyWriter.write(e.toString); 72 } 73 } 74 catch (Throwable) {} 75 } 76 77 /** 78 * Handles unhandled exceptions. 79 * Params: 80 * e = The unhandled exception. 81 */ 82 void handleUnhandledException(Exception e) 83 { 84 print("unhandledException: %s", e); 85 } 86 87 /** 88 * Handles user errors. 89 * Params: 90 * t = The throwable error, if any. 91 * request = The request. 92 * response = The response. 93 * error = The error information, if any. 94 */ 95 void handleUserError(Throwable t, HTTPServerRequest request, HTTPServerResponse response, HTTPServerErrorInfo error) 96 { 97 try 98 { 99 response.statusCode = (error ? error.code : 500); 100 101 if (error && error.code == 404) 102 { 103 response.statusCode = 404; 104 105 foreach (headerKey,headerValue; webConfig.defaultHeaders.notFound) 106 { 107 response.headers[headerKey] = headerValue; 108 } 109 110 if (webSettings) 111 { 112 webSettings.onNotFound(request,response); 113 } 114 else 115 { 116 response.bodyWriter.write("Not found ..."); 117 } 118 return; 119 } 120 121 foreach (headerKey,headerValue; webConfig.defaultHeaders.error) 122 { 123 response.headers[headerKey] = headerValue; 124 } 125 126 if (response.statusCode != 404 && response.statusCode != 200) 127 { 128 // log ... 129 } 130 131 if (webSettings) 132 { 133 webSettings.onHttpError(t,request,response,error); 134 } 135 else 136 { 137 response.bodyWriter.write(t.toString); 138 } 139 } 140 catch (Throwable) {} 141 } 142 143 /** 144 * Handles unhandled errors. 145 * Params: 146 * t = The unhandled throwable error. 147 */ 148 void handleUnhandledError(Throwable t) 149 { 150 print("unhandledError: %s", t); 151 } 152 }