[
https://issues.apache.org/jira/browse/MRESOLVER-600?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17878422#comment-17878422
]
ASF GitHub Bot commented on MRESOLVER-600:
------------------------------------------
doddi commented on code in PR #576:
URL: https://github.com/apache/maven-resolver/pull/576#discussion_r1740083243
##########
maven-resolver-spi/src/main/java/org/eclipse/aether/spi/connector/transport/http/RFC9457/Rfc9457Reporter.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.eclipse.aether.spi.connector.transport.http.RFC9457;
+
+import java.io.IOException;
+
+/**
+ * A reporter for RFC9457 messages.
+ * RFC9457 is a standard for reporting problems in HTTP responses as a JSON
object.
+ * There are members specified in the RFC but none of those appear to be
required,
+ * @see <a href=https://www.rfc-editor.org/rfc/rfc9457#section-3-7>rfc9457
section 3.7</a>
+ * Given the JSON fields are not mandatory, this reporter simply extracts the
body of the
+ * response without validation.
+ * A RFC9457 message is detected by the content type
"application/problem+json".
+ *
+ * @param <T> The type of the response.
+ * @param <E> The base exception type to throw if the response is not a
RFC9457 message.
+ */
+public abstract class Rfc9457Reporter<T, E extends Exception> {
+ protected abstract boolean isRfc9457Message(T response);
+
+ protected abstract int getStatusCode(T response);
+
+ protected abstract String getReasonPhrase(T response);
+
+ protected abstract String getBody(T response) throws IOException;
+
+ protected boolean hasRfc9457ContentType(String contentType) {
+ return contentType != null &&
contentType.equals("application/problem+json");
+ }
+ /**
+ * Generates a {@link HttpRfc9457Exception} if the response type is a
RFC9457 message.
+ * Otherwise, it throws the base exception
+ *
+ * @param request The response to check for RFC9457 messages.
+ * @param baseException The base exception to throw if the response is not
a RFC9457 message.
+ */
+ public void generateException(T request, BiConsumerChecked<Integer,
String, E> baseException)
+ throws HttpRfc9457Exception, E {
+ int statusCode = getStatusCode(request);
+ String reasonPhrase = getReasonPhrase(request);
+ if (isRfc9457Message(request)) {
+ try {
+ /* The rfc9457 does not specify a structure to the payload, so
we can't
+ really do anything with it other than add the content to the
exception. */
+ String body = getBody(request);
+ boolean hasReasonPhrase = reasonPhrase != null &&
!reasonPhrase.isEmpty();
+ boolean hasBody = body != null && !body.isEmpty();
+
+ String message = String.format("status code: %d", statusCode);
+ if (hasReasonPhrase) {
+ message += String.format(", reason phrase: %s",
reasonPhrase);
+ }
+ if (hasBody) {
+ message += String.format(", message: %s", body);
+ }
+
+ throw new HttpRfc9457Exception(statusCode, reasonPhrase,
message);
+ } catch (IOException ignored) {
+ }
+ }
+ baseException.accept(statusCode, reasonPhrase);
Review Comment:
If the response is not an RFC9457 type then throw the original exception,
which is different (including the message) for each transporter.
> Implement RFC9457
> -----------------
>
> Key: MRESOLVER-600
> URL: https://issues.apache.org/jira/browse/MRESOLVER-600
> Project: Maven Resolver
> Issue Type: New Feature
> Components: Resolver
> Reporter: Mark Dodgson
> Priority: Minor
>
> HTTP1.1 [RFC7230|https://www.rfc-editor.org/rfc/rfc7230#section-3.1.2]
> section 3.1.2 defines the response status code to optionally include a text
> description (human readable) of the reason for the status code. A more
> detailed RFC is
> [RFC2616|https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html]
> There is an additional [RFC9457|https://www.rfc-editor.org/rfc/rfc9457] which
> makes use of the body to inform of a reason for the error response allowing
> for easier investigation.
> h2. Why is this important
> [RFC9113|https://datatracker.ietf.org/doc/html/rfc9113] is the HTTP2 protocol
> standard and the response status only considers the [status
> code|https://www.rfc-editor.org/rfc/rfc9113#name-response-pseudo-header-fiel]
> and not the reason phrase, as such important information can be lost in
> helping the client determine a route cause of a failure.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)