-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerSlf4j.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
core/src/main/java/io/github/mmm/base/exception/GlobalExceptionHandlerSysout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters