Improve allowNullValue handling when a null value is provided

This commit improves `AbstractValueAdaptingCache` to throw a dedicated
exception if `allowNullValues` is `false` and a `null` value is provided
anyway. This avoid a lower-level exception from the cache library that
will miss some context.

Issue: SPR-15173
This commit is contained in:
Stephane Nicoll
2017-02-20 15:58:04 +01:00
parent 2fe5064dbe
commit 1c74a1a0fe
8 changed files with 109 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -95,8 +95,13 @@ public abstract class AbstractValueAdaptingCache implements Cache {
* @return the value to store
*/
protected Object toStoreValue(Object userValue) {
if (this.allowNullValues && userValue == null) {
return NullValue.INSTANCE;
if (userValue == null) {
if (this.allowNullValues) {
return NullValue.INSTANCE;
}
throw new IllegalArgumentException(
String.format("Cache '%s' is configured to not allow null " +
"values but null was provided", getName()));
}
return userValue;
}