From 3bc20f50872a216a92d9c1953c7c7d9d27f7575b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Hohwiller?= Date: Wed, 4 Oct 2023 22:55:31 +0200 Subject: [PATCH] add GlobalExceptionHandler --- core/pom.xml | 7 +++++ .../exception/GlobalExceptionHandler.java | 25 +++++++++++++++++ .../GlobalExceptionHandlerAccess.java | 27 +++++++++++++++++++ .../GlobalExceptionHandlerSlf4j.java | 19 +++++++++++++ .../GlobalExceptionHandlerSysout.java | 14 ++++++++++ core/src/main/java/module-info.java | 9 +++++++ 6 files changed, 101 insertions(+) create mode 100644 core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandler.java create mode 100644 core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerAccess.java create mode 100644 core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerSlf4j.java create mode 100644 core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerSysout.java diff --git a/core/pom.xml b/core/pom.xml index 3906d84..accdac9 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -11,4 +11,11 @@ jar ${project.artifactId} Basic interfaces and classes for reuse. + + + org.slf4j + slf4j-api + true + + diff --git a/core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandler.java b/core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..bf314c6 --- /dev/null +++ b/core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandler.java @@ -0,0 +1,25 @@ +/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 */ +package io.github.mmm.base.exception; + +/** + * Interface used as fallback to {@link #handleError(Object, Throwable) handle an error} that can not be handled in a + * specific way by generic components. It allows to write portable code that can delegate error handling to this + * interface allowing to exchange its implementation and therefore the handling strategy. + */ +public interface GlobalExceptionHandler { + + /** + * This method handles an error that occurred in a generic component that can not handle it in a specific way.
+ * In a typical server application you may like to log the errors while in a client application you might want to show + * a popup that displays the error. + * + * @param context is an Object with information about the context when the error occurred. Its + * {@link Object#toString() string representation} should be human readable and give additional hints to track + * down the error. E.g. the source or parameters of an operation where the error occurred. This parameter may + * also be {@code null} if no context information is available. + * @param error is the {@link Throwable error} that has been catched and shall be handled. + */ + void handleError(Object context, Throwable error); + +} \ No newline at end of file diff --git a/core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerAccess.java b/core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerAccess.java new file mode 100644 index 0000000..d1236e5 --- /dev/null +++ b/core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerAccess.java @@ -0,0 +1,27 @@ +package io.github.mmm.base.exception; + +import java.util.ServiceLoader; + +import io.github.mmm.base.config.ServiceHelper; + +/** + * Class giving {@link #get() global access} to the {@link GlobalExceptionHandler} implementation. + */ +public final class GlobalExceptionHandlerAccess { + + private static final GlobalExceptionHandler HANDLER = ServiceHelper + .singleton(ServiceLoader.load(GlobalExceptionHandler.class), false); + + private GlobalExceptionHandlerAccess() { + + } + + /** + * @return the {@link GlobalExceptionHandler} instance. + */ + public static GlobalExceptionHandler get() { + + return HANDLER; + } + +} diff --git a/core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerSlf4j.java b/core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerSlf4j.java new file mode 100644 index 0000000..5a55029 --- /dev/null +++ b/core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerSlf4j.java @@ -0,0 +1,19 @@ +package io.github.mmm.base.exception; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Implementation of {@link GlobalExceptionHandler} using SLF4J {@link Logger}. + */ +public class GlobalExceptionHandlerSlf4j implements GlobalExceptionHandler { + + private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandlerSlf4j.class); + + @Override + public void handleError(Object context, Throwable error) { + + LOG.warn("Unhandeled error in {}", context, error); + } + +} diff --git a/core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerSysout.java b/core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerSysout.java new file mode 100644 index 0000000..09d815a --- /dev/null +++ b/core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerSysout.java @@ -0,0 +1,14 @@ +package io.github.mmm.base.exception; + +/** + * Implementation of {@link GlobalExceptionHandler} that simply performs a {@link Throwable#printStackTrace()}. + */ +public class GlobalExceptionHandlerSysout implements GlobalExceptionHandler { + + @Override + public void handleError(Object context, Throwable error) { + + error.printStackTrace(); + } + +} diff --git a/core/src/main/java/module-info.java b/core/src/main/java/module-info.java index 3c2d42e..211d80f 100644 --- a/core/src/main/java/module-info.java +++ b/core/src/main/java/module-info.java @@ -7,15 +7,24 @@ * Provides fundamental APIs and helper classes. * * @provides io.github.mmm.base.temporal.TemporalConverter + * @provides io.github.mmm.base.exception.GlobalExceptionHandler + * @uses io.github.mmm.base.exception.GlobalExceptionHandler * @uses io.github.mmm.base.temporal.TemporalConverter */ module io.github.mmm.base { + requires static org.slf4j; + uses io.github.mmm.base.temporal.TemporalConverter; provides io.github.mmm.base.temporal.TemporalConverter with // io.github.mmm.base.temporal.impl.TemporalConverterImpl; + uses io.github.mmm.base.exception.GlobalExceptionHandler; + + provides io.github.mmm.base.exception.GlobalExceptionHandler + with io.github.mmm.base.exception.GlobalExceptionHandlerSysout; + exports io.github.mmm.base.collection; exports io.github.mmm.base.compare;