Grails 2.4.x はこちら。
によって生成されたすべての Grails サービスのすべてのメソッドをgrails create-service <xyz>
、次のロジックで「ラップ」/インターセプトする必要があります。
try {
executeTheMethod()
} catch(MyAppException maExc) {
log.error(ExceptionUtils.getStackTrace(maExc))
myAppExceptionHandler.handleOrRethrow(maExc)
}
どこ:
log.error(...)
クラスに注釈を付けるときに取得する SLF4J 提供のロガーです@Slf4j
。とExceptionUtils
からのものorg.apache.commons:commons-lang3:3.4
です。とmyAppExceptionHandler
タイプcom.example.myapp.MyAppExceptionHandler
です。と- この動作は、Grails サービスで定義された各メソッドに存在します (または、何らかの形で明示的に呼び出す必要がある場合に存在するオプションがあります)。
したがって、明らかにこのラッパー コードにはimport
、これらのクラスのステートメントも含める必要があります。
たとえば、WidgetService
次のようながあるとします。
class WidgetService {
WidgetDataService widgetDataService = new WidgetDataService()
Widget getWidgetById(Long widgetId) {
List<Widget> widgets = widgetDataService.getAllWidgets()
widgets.each {
if(it.id.equals(widgetId)) {
return it
}
}
return null
}
}
次に、この Groovy/Grails/クロージャ マジックが発生した後、次のように記述したかのようにコードを動作させる必要があります。
import groovy.util.logging.Slf4j
import org.apache.commons.lang3.exception.ExceptionUtils
import com.example.myapp.MyAppExceptionHandler
@Slf4j
class WidgetService {
WidgetDataService widgetDataService = new WidgetDataService()
MyAppExceptionHandler myAppExceptionHandler = new MyAppExceptionHandler()
Widget getWidgetById(Long widgetId) {
try {
List<Widget> widgets = widgetDataService.getAllWidgets()
widgets.each {
if(it.id.equals(widgetId)) {
return it
}
}
return null
} catch(MyAppException maExc) {
log.error(ExceptionUtils.getStackTrace(maExc))
myAppExceptionHandler.handleOrRethrow(maExc)
}
}
}
どうすればこれを達成できるかについてのアイデアはありますか? 純粋な Groovy クロージャが、実行時に内部で Grails がそのサービスに対して行っていることと何らかの形で干渉するのではないかと心配しています (それらはすべて親クラスを明示的に拡張しないクラスであるため)。