Skip to content

Commit

Permalink
add GlobalExceptionHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Oct 4, 2023
1 parent 7eb2804 commit 3bc20f5
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>Basic interfaces and classes for reuse.</description>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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. <br>
* 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);

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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();
}

}
9 changes: 9 additions & 0 deletions core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3bc20f5

Please sign in to comment.