Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import org.schabi.newpipe.extractor.exceptions.HttpResponseException;

/**
* A Data class used to hold the results from requests made by the Downloader implementation.
*/
@SuppressWarnings("checkstyle:NeedBraces")
public class Response {
private final int responseCode;
private final String responseMessage;
Expand Down Expand Up @@ -80,4 +84,129 @@

return null;
}

/**
* Ensure the response code is 2xx
* @return this {@code Response}
* @throws HttpResponseException if the response code is not 2xx
*/
public Response ensureSuccessResponseCode() throws HttpResponseException {
return ensureResponseCodeInRange(200, 299);
}

/**
* Ensure the response code is 3xx
* @return this {@code Response}
* @throws HttpResponseException if the response code is not 3xx
*/
public Response ensureRedirectResponseCode() throws HttpResponseException {
return ensureResponseCodeInRange(300, 399);
}

/**
* Ensure the response code is not 4xx or 5xx
* @return this {@code Response}
* @throws HttpResponseException if the response code is client or server error
*/
public Response ensureResponseCodeIsNotError() throws HttpResponseException {
return ensureResponseCodeNotInRange(400, 599);
}

/**
* Ensure the HTTP response code is within range of min and max inclusive
* @return this Response
* @throws HttpResponseException if the code is outside the range
*/
public Response ensureResponseCodeInRange(final int min, final int max)
throws HttpResponseException {
if (responseCode() < min || responseCode() > max) {
throw new HttpResponseException(this);
}
return this;
}

public Response ensureResponseCodeNotInRange(
final int min,
final int max,
final Function<Response, HttpResponseException> errorSupplier
)
throws HttpResponseException {
if (min > max) throw new RuntimeException("min must be less than max");

Check warning on line 134 in extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace generic exceptions with specific library exceptions or a custom exception.

See more on https://sonarcloud.io/project/issues?id=TeamNewPipe_NewPipeExtractor&issues=AZ1mS8xGH6rvDwYnTJuP&open=AZ1mS8xGH6rvDwYnTJuP&pullRequest=1322
Comment thread
absurdlylongusername marked this conversation as resolved.
Outdated
if (responseCode() >= min && responseCode() <= max) {
throw errorSupplier.apply(this);
}
return this;
}

public Response ensureResponseCodeNotInRange(final int min, final int max)
throws HttpResponseException {
return ensureResponseCodeNotInRange(min, max, HttpResponseException::new);
}

/**
* Throw exception if response code is a 4xx client error
* @return this {@code Response}
* @throws HttpResponseException if the response code is 4xx
*/
public Response throwIfClientError(
final Function<Response, HttpResponseException> errorSupplier
)
throws HttpResponseException {
return throwIfResponseCodeInRange(400, 499, errorSupplier);
}

/**
* Throw exception if response code is a 4xx client error
* @return this {@code Response}
* @throws HttpResponseException if the response code is 4xx
*/
public Response throwIfClientError()
throws HttpResponseException {
return throwIfClientError(HttpResponseException::new);
}

/**
* Throw exception if response code is a 5xx server error
* @return this {@code Response}
* @throws HttpResponseException if the response code is 4xx
Comment thread
absurdlylongusername marked this conversation as resolved.
Outdated
*/
public Response throwIfServerError(
final Function<Response, HttpResponseException> errorSupplier
)
throws HttpResponseException {
return throwIfResponseCodeInRange(500, 599, errorSupplier);
}

public Response throwIfServerError()
throws HttpResponseException {
return throwIfServerError(HttpResponseException::new);
}

public Response throwIfResponseCode(
final int errorCode,
final Function<Response, HttpResponseException> errorSupplier
)
throws HttpResponseException {
if (responseCode() == errorCode) {
throw errorSupplier.apply(this);
}
return this;
}

public Response throwIfResponseCode(final int errorCode) throws HttpResponseException {
return throwIfResponseCode(errorCode, HttpResponseException::new);
}

public Response throwIfResponseCodeInRange(final int min,
final int max,
final Function<Response,
HttpResponseException> errorSupplier)
throws HttpResponseException {
return ensureResponseCodeNotInRange(min, max, errorSupplier);
}

public Response throwIfResponseCodeInRange(final int min,
final int max) throws HttpResponseException {
return throwIfResponseCodeInRange(min, max, HttpResponseException::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.schabi.newpipe.extractor.exceptions;

import org.schabi.newpipe.extractor.downloader.Response;

public class HttpResponseException extends ExtractionException {
public HttpResponseException(final Response response) {
this("Error in HTTP Response for " + response.latestUrl() + "\n\t"
+ response.responseCode() + " - " + response.responseMessage());
}

public HttpResponseException(final String message) {
super(message);
}
}
Loading