Skip to content

Commit

Permalink
Merge pull request #98 from dbmdz/problem-hints
Browse files Browse the repository at this point in the history
Add `Problem` hints
  • Loading branch information
daforster authored Apr 23, 2024
2 parents 96042dd + b05b273 commit 520b3b6
Show file tree
Hide file tree
Showing 37 changed files with 253 additions and 64 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

### Added

- The `Problem` (of error responses) is expanded by a hint that gives clearer information about the actual error

## [9.1.0](https://github.com/dbmdz/metadata-service/releases/tag/9.1.0) - 2024-04-16

### Added
Expand Down
2 changes: 1 addition & 1 deletion metasvc-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.github.dbmdz.metadata</groupId>
<artifactId>metasvc</artifactId>
<version>9.1.1-SNAPSHOT</version>
<version>9.2.0-SNAPSHOT</version>
</parent>

<name>Metadata-Service: Repository Client</name>
Expand Down
2 changes: 1 addition & 1 deletion metasvc-lobid-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.dbmdz.metadata</groupId>
<artifactId>metasvc</artifactId>
<version>9.1.1-SNAPSHOT</version>
<version>9.2.0-SNAPSHOT</version>
</parent>

<name>Metadata-Service: lobid.org Client</name>
Expand Down
2 changes: 1 addition & 1 deletion metasvc-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.github.dbmdz.metadata</groupId>
<artifactId>metasvc</artifactId>
<version>9.1.1-SNAPSHOT</version>
<version>9.2.0-SNAPSHOT</version>
</parent>

<name>Metadata-Service: Model</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import de.digitalcollections.model.exception.http.server.HttpVersionNotSupportedException;
import de.digitalcollections.model.exception.http.server.NotImplementedException;
import de.digitalcollections.model.exception.http.server.ServiceUnavailableException;
import de.digitalcollections.model.exception.problem.MetasvcProblem;
import de.digitalcollections.model.jackson.DigitalCollectionsObjectMapper;
import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -23,7 +24,6 @@
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zalando.problem.Problem;
import org.zalando.problem.jackson.ProblemModule;

public class HttpErrorDecoder {
Expand All @@ -36,7 +36,7 @@ public class HttpErrorDecoder {
}

private static HttpException clientException(
String methodKey, int statusCode, String requestUrl, Problem problem) {
String methodKey, int statusCode, String requestUrl, MetasvcProblem problem) {
switch (statusCode) {
case 401:
return new UnauthorizedException(methodKey, statusCode, requestUrl, problem);
Expand All @@ -57,7 +57,7 @@ private static HttpException clientException(

public static HttpException decode(String methodKey, int statusCode, HttpResponse response) {
String requestUrl = null;
Problem problem = null;
MetasvcProblem problem = null;
if (response != null) {
requestUrl =
Optional.ofNullable(response.request())
Expand All @@ -77,7 +77,7 @@ public static HttpException decode(String methodKey, int statusCode, HttpRespons
final byte[] body = (byte[]) response.body();
if (body != null && body.length > 0) {
try {
problem = mapper.readerFor(Problem.class).readValue(body);
problem = mapper.readerFor(MetasvcProblem.class).readValue(body);
} catch (Exception e) {
LOGGER.error(
"Got response="
Expand All @@ -99,12 +99,12 @@ public static HttpException decode(String methodKey, int statusCode, HttpRespons
}

private static HttpException genericHttpException(
String methodKey, int statusCode, String requestUrl, Problem problem) {
String methodKey, int statusCode, String requestUrl, MetasvcProblem problem) {
return new HttpException(methodKey, statusCode, requestUrl, problem);
}

private static HttpServerException serverException(
String methodKey, int statusCode, String requestUrl, Problem problem) {
String methodKey, int statusCode, String requestUrl, MetasvcProblem problem) {
switch (statusCode) {
case 501:
return new NotImplementedException(methodKey, statusCode, requestUrl, problem);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package de.digitalcollections.model.exception.http;

import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

public class HttpException extends RuntimeException {

private final String methodKey;
private final String request;
private final Integer statuscode;
private final Problem problem;
private final MetasvcProblem problem;

public HttpException(String methodKey, Exception ex) {
super(String.format("Got exception for backend call %s.", methodKey), ex);
Expand All @@ -25,7 +25,7 @@ public HttpException(String methodKey, int statuscode) {
this.problem = null;
}

public HttpException(String methodKey, int statuscode, String request, Problem problem) {
public HttpException(String methodKey, int statuscode, String request, MetasvcProblem problem) {
super(String.format("Got %d for backend call %s.%n⤷ %s", statuscode, methodKey, request));
this.methodKey = methodKey;
this.request = request;
Expand All @@ -45,7 +45,7 @@ public Integer getStatusCode() {
return statuscode;
}

public Problem getProblem() {
public MetasvcProblem getProblem() {
return problem;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package de.digitalcollections.model.exception.http.client;

import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

public class ForbiddenException extends HttpClientException {

public ForbiddenException(String methodKey, int status, String request, Problem problem) {
public ForbiddenException(String methodKey, int status, String request, MetasvcProblem problem) {
super(methodKey, status, request, problem);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package de.digitalcollections.model.exception.http.client;

import de.digitalcollections.model.exception.http.HttpException;
import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

public class HttpClientException extends HttpException {

public HttpClientException(String methodKey, int status, String request, Problem problem) {
public HttpClientException(String methodKey, int status, String request, MetasvcProblem problem) {
super(methodKey, status, request, problem);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.digitalcollections.model.exception.http.client;

import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

/**
* HttpStatusCode 418 denoting the api is wrongfully using a teapot for making coffee as specified
Expand All @@ -9,7 +9,7 @@
*/
public class ImATeapotException extends HttpClientException {

public ImATeapotException(String methodKey, int status, String request, Problem problem) {
public ImATeapotException(String methodKey, int status, String request, MetasvcProblem problem) {
super(methodKey, status, request, problem);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package de.digitalcollections.model.exception.http.client;

import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

public class ResourceNotFoundException extends HttpClientException {

public ResourceNotFoundException(String methodKey, int status, String request, Problem problem) {
public ResourceNotFoundException(
String methodKey, int status, String request, MetasvcProblem problem) {
super(methodKey, status, request, problem);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package de.digitalcollections.model.exception.http.client;

import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

public class UnauthorizedException extends HttpClientException {

public UnauthorizedException(String methodKey, int status, String request, Problem problem) {
public UnauthorizedException(
String methodKey, int status, String request, MetasvcProblem problem) {
super(methodKey, status, request, problem);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package de.digitalcollections.model.exception.http.client;

import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

public class UnavailableForLegalReasonsException extends HttpClientException {

public UnavailableForLegalReasonsException(
String methodKey, int status, String request, Problem problem) {
String methodKey, int status, String request, MetasvcProblem problem) {
super(methodKey, status, request, problem);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package de.digitalcollections.model.exception.http.client;

import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

public class UnprocessableEntityException extends HttpClientException {

public UnprocessableEntityException(
String methodKey, int status, String request, Problem problem) {
String methodKey, int status, String request, MetasvcProblem problem) {
super(methodKey, status, request, problem);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package de.digitalcollections.model.exception.http.server;

import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

public class BadGatewayException extends HttpServerException {

public BadGatewayException(String methodKey, int status, String request, Problem problem) {
public BadGatewayException(String methodKey, int status, String request, MetasvcProblem problem) {
super(methodKey, status, request, problem);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package de.digitalcollections.model.exception.http.server;

import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

public class GatewayTimeOutException extends HttpServerException {

public GatewayTimeOutException(String methodKey, int status, String request, Problem problem) {
public GatewayTimeOutException(
String methodKey, int status, String request, MetasvcProblem problem) {
super(methodKey, status, request, problem);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package de.digitalcollections.model.exception.http.server;

import de.digitalcollections.model.exception.http.HttpException;
import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

public class HttpServerException extends HttpException {

public HttpServerException(String methodKey, int status, String request, Problem problem) {
public HttpServerException(String methodKey, int status, String request, MetasvcProblem problem) {
super(methodKey, status, request, problem);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package de.digitalcollections.model.exception.http.server;

import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

public class HttpVersionNotSupportedException extends HttpServerException {

public HttpVersionNotSupportedException(
String methodKey, int status, String request, Problem problem) {
String methodKey, int status, String request, MetasvcProblem problem) {
super(methodKey, status, request, problem);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package de.digitalcollections.model.exception.http.server;

import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

public class NotImplementedException extends HttpServerException {

public NotImplementedException(String methodKey, int status, String request, Problem problem) {
public NotImplementedException(
String methodKey, int status, String request, MetasvcProblem problem) {
super(methodKey, status, request, problem);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package de.digitalcollections.model.exception.http.server;

import org.zalando.problem.Problem;
import de.digitalcollections.model.exception.problem.MetasvcProblem;

public class ServiceUnavailableException extends HttpServerException {

public ServiceUnavailableException(
String methodKey, int status, String request, Problem problem) {
String methodKey, int status, String request, MetasvcProblem problem) {
super(methodKey, status, request, problem);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package de.digitalcollections.model.exception.problem;

import de.digitalcollections.model.validation.ValidationError;
import java.net.URI;
import java.util.Date;
import java.util.List;
import lombok.Builder;
import lombok.Singular;
import org.zalando.problem.AbstractThrowableProblem;
import org.zalando.problem.StatusType;

public final class MetasvcProblem extends AbstractThrowableProblem {

private List<ValidationError> errors;
private Date timestamp;
private ProblemHint hint;

public MetasvcProblem() {
super();
}

public MetasvcProblem(
URI type, String title, StatusType status, String detail, URI instance, Date timestamp) {
super(type, title, status, detail, instance);
this.timestamp = timestamp;
}

@Builder(setterPrefix = "with")
public MetasvcProblem(
URI type,
String title,
StatusType status,
String detail,
URI instance,
Date timestamp,
@Singular List<ValidationError> errors,
ProblemHint hint) {
super(type, title, status, detail, instance);
this.timestamp = timestamp;
this.errors = errors;
this.hint = hint;
}

public MetasvcProblem(
URI type,
String title,
StatusType status,
String detail,
URI instance,
Date timestamp,
ProblemHint hint) {
super(type, title, status, detail, instance);
this.timestamp = timestamp;
this.hint = hint;
}

public List<ValidationError> getErrors() {
return errors;
}

public Date getTimestamp() {
return timestamp;
}

public ProblemHint getHint() {
return hint;
}
}
Loading

0 comments on commit 520b3b6

Please sign in to comment.