-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: store proxy beans now include a store translation interceptor th… (
#1616) * fix: store proxy beans now include a store translation interceptor that re-throws RuntimeException as StoreAccessException - this provides a hook for stores to add their own translators * Store exception translator interceptor now supports store-provided translations through StoreExceptionTranslator beans * docs: add docs for StoreExceptionTranslator
- Loading branch information
1 parent
5c93df3
commit 43150b4
Showing
6 changed files
with
169 additions
and
169 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
39 changes: 39 additions & 0 deletions
39
...rg/springframework/content/commons/store/factory/StoreExceptionTranslatorInterceptor.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,39 @@ | ||
package internal.org.springframework.content.commons.store.factory; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.content.commons.store.StoreAccessException; | ||
import org.springframework.content.commons.store.StoreExceptionTranslator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class StoreExceptionTranslatorInterceptor implements MethodInterceptor { | ||
private final BeanFactory beanFactory; | ||
private List<StoreExceptionTranslator> translators = null; | ||
|
||
public StoreExceptionTranslatorInterceptor(BeanFactory beanFactory) { | ||
this.beanFactory = beanFactory; | ||
} | ||
|
||
@Override | ||
public Object invoke(MethodInvocation invocation) throws Throwable { | ||
try { | ||
return invocation.proceed(); | ||
} catch (RuntimeException re) { | ||
if (re instanceof StoreAccessException) { | ||
throw re; | ||
} | ||
if (translators == null) { | ||
translators = new ArrayList<>(); | ||
beanFactory.getBeanProvider(StoreExceptionTranslator.class).orderedStream().forEach(translators::add); | ||
} | ||
StoreAccessException sae = null; | ||
for (int i=0; i < translators.size() && sae == null; i++) { | ||
sae = translators.get(i).translate(re); | ||
} | ||
throw sae != null ? sae : re; | ||
} | ||
} | ||
} |
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
6 changes: 6 additions & 0 deletions
6
...ons/src/main/java/org/springframework/content/commons/store/StoreExceptionTranslator.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,6 @@ | ||
package org.springframework.content.commons.store; | ||
|
||
@FunctionalInterface | ||
public interface StoreExceptionTranslator { | ||
StoreAccessException translate(RuntimeException re); | ||
} |
Oops, something went wrong.