1 /** 2 * Copyright © DiamondMVC 2018 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 auto httpStatusExcepton = cast(HTTPStatusException)e; 34 35 if ((!httpStatusExcepton || httpStatusExcepton.status != HTTPStatus.NotFound) && 36 (response.statusCode != 404 && response.statusCode != 200) 37 ) 38 { 39 // log ... 40 } 41 42 if (httpStatusExcepton && httpStatusExcepton.status == HTTPStatus.NotFound) 43 { 44 response.statusCode = 404; 45 46 foreach (headerKey,headerValue; webConfig.defaultHeaders.notFound) 47 { 48 response.headers[headerKey] = headerValue; 49 } 50 51 if (webSettings) 52 { 53 webSettings.onNotFound(request,response); 54 } 55 else 56 { 57 response.bodyWriter.write("Not found ..."); 58 } 59 return; 60 } 61 62 foreach (headerKey,headerValue; webConfig.defaultHeaders.error) 63 { 64 response.headers[headerKey] = headerValue; 65 } 66 67 if (webSettings) 68 { 69 webSettings.onHttpError(e,request,response,error); 70 } 71 else 72 { 73 response.bodyWriter.write(e.toString); 74 } 75 } 76 catch (Throwable) {} 77 } 78 79 /** 80 * Handles unhandled exceptions. 81 * Params: 82 * e = The unhandled exception. 83 */ 84 void handleUnhandledException(Exception e) 85 { 86 print("unhandledException: %s", e); 87 } 88 89 /** 90 * Handles user errors. 91 * Params: 92 * t = The throwable error, if any. 93 * request = The request. 94 * response = The response. 95 * error = The error information, if any. 96 */ 97 void handleUserError(Throwable t, HTTPServerRequest request, HTTPServerResponse response, HTTPServerErrorInfo error) 98 { 99 try 100 { 101 response.statusCode = error ? error.code : 500; 102 103 if (error && error.code == 404) 104 { 105 response.statusCode = 404; 106 107 foreach (headerKey,headerValue; webConfig.defaultHeaders.notFound) 108 { 109 response.headers[headerKey] = headerValue; 110 } 111 112 if (webSettings) 113 { 114 webSettings.onNotFound(request,response); 115 } 116 else 117 { 118 response.bodyWriter.write("Not found ..."); 119 } 120 return; 121 } 122 123 foreach (headerKey,headerValue; webConfig.defaultHeaders.error) 124 { 125 response.headers[headerKey] = headerValue; 126 } 127 128 if (response.statusCode != 404 && response.statusCode != 200) 129 { 130 // log ... 131 } 132 133 if (webSettings) 134 { 135 webSettings.onHttpError(t,request,response,error); 136 } 137 else 138 { 139 response.bodyWriter.write(t.toString); 140 } 141 } 142 catch (Throwable) {} 143 } 144 145 /** 146 * Handles unhandled errors. 147 * Params: 148 * t = The unhandled throwable error. 149 */ 150 void handleUnhandledError(Throwable t) 151 { 152 print("unhandledError: %s", t); 153 } 154 }