Skip to content

Commit

Permalink
Added simple filter, for error page and not found page. (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianKirmaier authored Jan 15, 2025
1 parent 80dd5c5 commit cf29e30
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,26 @@ object Filters {
route.and(function)
}

def errorPage(): Filter = errorPage((request, ex) => Response.node(new Label("Error: " + ex.getMessage)))
def errorPage(biFunction: BiFunction[Request, Throwable, Response]): Filter = {
route => { request =>
try {
val r = route.apply(request)
Response.fromFuture(r.future.map(x => Response.fromResult(x)).exceptionally { ex =>
biFunction.apply(request, ex)
})
} catch {
case ex: Throwable =>
biFunction.apply(request, ex)
}
}
}

def notFoundPage(): Filter = {
notFoundPage((request) => Response.node(new Label("Not Found: " + request.getPath())))
}
def notFoundPage(function: Route): Filter = { route =>
route.and(function)
}

}

0 comments on commit cf29e30

Please sign in to comment.