Skip to content

Commit 7017cba

Browse files
authored
Pass request to error handlers (#133)
This allows customizing error responses based on data available in requests. One scenario would be to display a 404, but with additional context information, e.g. the path or login status of the user taken from cookies.
1 parent a6719f6 commit 7017cba

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

cask/src/cask/main/Main.scala

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ abstract class Main{
5050
new Main.DefaultHandler(dispatchTrie, mainDecorators, debugMode, handleNotFound, handleMethodNotAllowed, handleEndpointError)
5151
)
5252

53-
def handleNotFound() = Main.defaultHandleNotFound()
53+
def handleNotFound(req: Request): Response.Raw = Main.defaultHandleNotFound(req)
5454

55-
def handleMethodNotAllowed() = Main.defaultHandleMethodNotAllowed()
55+
def handleMethodNotAllowed(req: Request): Response.Raw = Main.defaultHandleMethodNotAllowed(req)
5656

5757
def handleEndpointError(routes: Routes,
5858
metadata: EndpointMetadata[_],
59-
e: cask.router.Result.Error) = {
60-
Main.defaultHandleError(routes, metadata, e, debugMode)
59+
e: cask.router.Result.Error,
60+
req: Request): Response.Raw = {
61+
Main.defaultHandleError(routes, metadata, e, debugMode, req)
6162
}
6263

6364
def main(args: Array[String]): Unit = {
@@ -75,9 +76,9 @@ object Main{
7576
class DefaultHandler(dispatchTrie: DispatchTrie[Map[String, (Routes, EndpointMetadata[_])]],
7677
mainDecorators: Seq[Decorator[_, _, _]],
7778
debugMode: Boolean,
78-
handleNotFound: () => Response.Raw,
79-
handleMethodNotAllowed: () => Response.Raw,
80-
handleError: (Routes, EndpointMetadata[_], Result.Error) => Response.Raw)
79+
handleNotFound: Request => Response.Raw,
80+
handleMethodNotAllowed: Request => Response.Raw,
81+
handleError: (Routes, EndpointMetadata[_], Result.Error, Request) => Response.Raw)
8182
(implicit log: Logger) extends HttpHandler() {
8283
def handleRequest(exchange: HttpServerExchange): Unit = try {
8384
// println("Handling Request: " + exchange.getRequestPath)
@@ -106,13 +107,14 @@ object Main{
106107
.toList
107108

108109
dispatchTrie.lookup(decodedSegments, Map()) match {
109-
case None => Main.writeResponse(exchange, handleNotFound())
110+
case None => Main.writeResponse(exchange, handleNotFound(Request(exchange, decodedSegments)))
110111
case Some((methodMap, routeBindings, remaining)) =>
111112
methodMap.get(effectiveMethod) match {
112-
case None => Main.writeResponse(exchange, handleMethodNotAllowed())
113+
case None => Main.writeResponse(exchange, handleMethodNotAllowed(Request(exchange, remaining)))
113114
case Some((routes, metadata)) =>
115+
val req = Request(exchange, remaining)
114116
Decorator.invoke(
115-
Request(exchange, remaining),
117+
req,
116118
metadata.endpoint,
117119
metadata.entryPoint.asInstanceOf[EntryPoint[Routes, _]],
118120
routes,
@@ -124,7 +126,7 @@ object Main{
124126
case e: Result.Error =>
125127
Main.writeResponse(
126128
exchange,
127-
handleError(routes, metadata, e)
129+
handleError(routes, metadata, e, req)
128130
)
129131
}
130132
}
@@ -134,14 +136,14 @@ object Main{
134136
}
135137
}
136138

137-
def defaultHandleNotFound(): Response.Raw = {
139+
def defaultHandleNotFound(req: Request): Response.Raw = {
138140
Response(
139141
s"Error 404: ${Status.codesToStatus(404).reason}",
140142
statusCode = 404
141143
)
142144
}
143145

144-
def defaultHandleMethodNotAllowed(): Response.Raw = {
146+
def defaultHandleMethodNotAllowed(req: Request): Response.Raw = {
145147
Response(
146148
s"Error 405: ${Status.codesToStatus(405).reason}",
147149
statusCode = 405
@@ -199,7 +201,8 @@ object Main{
199201
def defaultHandleError(routes: Routes,
200202
metadata: EndpointMetadata[_],
201203
e: Result.Error,
202-
debugMode: Boolean)
204+
debugMode: Boolean,
205+
req: Request)
203206
(implicit log: Logger) = {
204207
e match {
205208
case e: Result.Error.Exception => log.exception(e.t)

0 commit comments

Comments
 (0)