-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check persistence metadata when sorting
Only persistent fields can be queried and sorted by. Trying to sort by a non-persistent field will cause an exception to be thrown in the persistence layer. To users it is not immediately obvious which field is persistent, and which is computed. It would be good to provide a helpful error message as to what went wrong when a sorting request can not be fulfilled. Instead of checking the candidate classes' fields directly, leverage the type metadata of DataNucleus to figure out which field is persistent. Use specific exception messages for whether a field is not persistent, or doesn't exist at all. Applications built on Alpine can then define a custom `ExceptionMapper` to transform the new `NotSortableException` into an API-friendly format if desired. Alternatively, it can be handled with any generic `IllegalArgumentException` handling logic. Signed-off-by: nscuro <[email protected]>
- Loading branch information
Showing
2 changed files
with
79 additions
and
11 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
46 changes: 46 additions & 0 deletions
46
alpine-infra/src/main/java/alpine/persistence/NotSortableException.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,46 @@ | ||
/* | ||
* This file is part of Alpine. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) Steve Springett. All Rights Reserved. | ||
*/ | ||
package alpine.persistence; | ||
|
||
public class NotSortableException extends IllegalArgumentException { | ||
|
||
private final String resourceName; | ||
private final String fieldName; | ||
private final String reason; | ||
|
||
NotSortableException(final String resourceName, final String fieldName, final String reason) { | ||
super("Can not sort by %s#%s: %s".formatted(resourceName, fieldName, reason)); | ||
this.resourceName = resourceName; | ||
this.fieldName = fieldName; | ||
this.reason = reason; | ||
} | ||
|
||
public String getResourceName() { | ||
return resourceName; | ||
} | ||
|
||
public String getFieldName() { | ||
return fieldName; | ||
} | ||
|
||
public String getReason() { | ||
return reason; | ||
} | ||
|
||
} |