-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add specific error code to REST API (#2443)
* Add specific error code to REST API * Add `errorCode` to the `NessieError` object, whose JSON representation is set to clients as HTTP error response payload. This is necessary to allow clients to distinguish Nessie-specific failure mode that fall under the same HTTP status code. * Note: this will break Nessie error response handling in older java clients. * Add more specific exception classes to `nessie-model` * Update java client to unwrap and throw (more) specific exceptions based on `NessieError` payload error codes. * Corresponding Python client changes to follow in a separate commit. * Fix a `TreeApiImpl.meta()` to throw a NessieIllegalArgumentException instead of NessieConflictException when the committer is set by the user. * Refactor `toHash()` in `TreeApiImpl` to remove unnecessary `Optional` * Minor code cleanup in `AbstractTestRest` * Make NessieError @Value.Immutable Closes #477
- Loading branch information
Showing
24 changed files
with
568 additions
and
291 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
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
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
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
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
model/src/main/java/org/projectnessie/error/ErrorCode.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 @@ | ||
/* | ||
* Copyright (C) 2020 Dremio | ||
* | ||
* 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. | ||
*/ | ||
package org.projectnessie.error; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Defines Nessie error codes that are more fine-grained than HTTP status codes and maps them to | ||
* exception classes. | ||
* | ||
* <p>The enum names also designate error code in the JSON representation of {@link NessieError}. | ||
*/ | ||
public enum ErrorCode { | ||
UNKNOWN(null), | ||
REFERENCE_NOT_FOUND(NessieReferenceNotFoundException::new), | ||
REFERENCE_ALREADY_EXISTS(NessieReferenceAlreadyExistsException::new), | ||
CONTENTS_NOT_FOUND(NessieContentsNotFoundException::new), | ||
REFERENCE_CONFLICT(NessieReferenceConflictException::new), | ||
; | ||
|
||
private final Function<NessieError, ? extends BaseNessieClientServerException> exceptionBuilder; | ||
|
||
<T extends BaseNessieClientServerException> ErrorCode(Function<NessieError, T> exceptionBuilder) { | ||
this.exceptionBuilder = exceptionBuilder; | ||
} | ||
|
||
public static Optional<BaseNessieClientServerException> asException(NessieError error) { | ||
return Optional.ofNullable(error.getErrorCode()) | ||
.flatMap(e -> Optional.ofNullable(e.exceptionBuilder)) | ||
.map(b -> b.apply(error)); | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
model/src/main/java/org/projectnessie/error/NessieContentsNotFoundException.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,35 @@ | ||
/* | ||
* Copyright (C) 2020 Dremio | ||
* | ||
* 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. | ||
*/ | ||
package org.projectnessie.error; | ||
|
||
import org.projectnessie.model.ContentsKey; | ||
|
||
/** This exception is thrown when the requested contents object is not present in the store. */ | ||
public class NessieContentsNotFoundException extends NessieNotFoundException { | ||
|
||
public NessieContentsNotFoundException(ContentsKey key, String ref) { | ||
super(String.format("Could not find contents for key '%s' in reference '%s'.", key, ref)); | ||
} | ||
|
||
public NessieContentsNotFoundException(NessieError error) { | ||
super(error); | ||
} | ||
|
||
@Override | ||
public ErrorCode getErrorCode() { | ||
return ErrorCode.CONTENTS_NOT_FOUND; | ||
} | ||
} |
Oops, something went wrong.