Skip to content

Commit

Permalink
Consistent Exception Handling for "pre-controller" request exceptions (
Browse files Browse the repository at this point in the history
  • Loading branch information
lbwexler authored Apr 23, 2024
1 parent 1e12848 commit 622e1bd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/main/groovy/io/xh/hoist/HoistCoreGrailsPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package io.xh.hoist
import grails.plugins.Plugin
import io.xh.hoist.cluster.ClusterService
import io.xh.hoist.exception.ExceptionHandler
import io.xh.hoist.security.HoistSecurityFilter
import io.xh.hoist.websocket.HoistWebSocketConfigurer
import org.springframework.boot.web.servlet.FilterRegistrationBean
import org.springframework.core.Ordered
Expand All @@ -37,8 +36,8 @@ class HoistCoreGrailsPlugin extends Plugin {
{->
ClusterService.initializeInstance()

hoistIdentityFilter(FilterRegistrationBean) {
filter = bean(HoistSecurityFilter)
hoistFilter(FilterRegistrationBean) {
filter = bean(HoistFilter)
order = Ordered.HIGHEST_PRECEDENCE + 40
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@
* Copyright © 2023 Extremely Heavy Industries Inc.
*/

package io.xh.hoist.security
package io.xh.hoist

import groovy.transform.CompileStatic
import io.xh.hoist.exception.ExceptionHandler
import io.xh.hoist.exception.InstanceNotAvailableException
import io.xh.hoist.log.LogSupport
import io.xh.hoist.security.BaseAuthenticationService
import io.xh.hoist.util.Utils

import javax.servlet.*
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
* Main Filter for all requests in Hoist.
*
* This filter is installed by Hoist Core with very high preference and is designed to
* precede/wrap the built-in grails filters.
*
* Implements security, app ready checking, and catches uncaught exceptions.
*/
@CompileStatic
class HoistSecurityFilter implements Filter, LogSupport {
class HoistFilter implements Filter, LogSupport {
void init(FilterConfig filterConfig) {}
void destroy() {}

Expand All @@ -28,18 +36,25 @@ class HoistSecurityFilter implements Filter, LogSupport {

// Need to be *ready* before even attempting auth.
if (!Utils.instanceReady) {
ExceptionHandler exceptionHandler = Utils.exceptionHandler
exceptionHandler.handleException(
Utils.exceptionHandler.handleException(
exception: new InstanceNotAvailableException('Application may be initializing. Please try again shortly.'),
renderTo: httpResponse,
logTo: this
)
return
}

BaseAuthenticationService svc = Utils.authenticationService
if (svc.allowRequest(httpRequest, httpResponse)) {
chain.doFilter(request, response)
BaseAuthenticationService authSvc = Utils.authenticationService
if (authSvc.allowRequest(httpRequest, httpResponse)) {
try {
chain.doFilter(request, response)
} catch (Throwable t) {
Utils.exceptionHandler.handleException(
exception: t,
renderTo: httpResponse,
logTo: this
)
}
}
}
}

0 comments on commit 622e1bd

Please sign in to comment.