Throw 400 response for invalid parameters messages from extractor.

This commit is contained in:
Kavin 2022-10-31 11:50:29 +00:00
parent afd213b4c8
commit d5585738da
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
2 changed files with 28 additions and 6 deletions

View file

@ -25,11 +25,23 @@ public class ErrorResponse extends Exception {
this.content = mapper.writeValueAsBytes(statusObj);
}
public ErrorResponse(IStatusCode statusObj, Throwable throwable) throws JsonProcessingException {
super(throwable);
this.code = statusObj.getStatusCode();
this.content = mapper.writeValueAsBytes(statusObj);
}
public ErrorResponse(int code, Object content) throws JsonProcessingException {
this.code = code;
this.content = mapper.writeValueAsBytes(content);
}
public ErrorResponse(int code, Object content, Throwable throwable) throws JsonProcessingException {
super(throwable);
this.code = code;
this.content = mapper.writeValueAsBytes(content);
}
public int getCode() {
return code;
}

View file

@ -3,8 +3,10 @@ package me.kavin.piped.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.sentry.Sentry;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.resp.InvalidRequestResponse;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
@ -20,13 +22,21 @@ public class ExceptionHandler {
if (e.getCause() != null && (e instanceof ExecutionException || e instanceof CompletionException))
e = (Exception) e.getCause();
if (!(e instanceof ContentNotAvailableException || e instanceof ErrorResponse)) {
Sentry.captureException(e);
if (Constants.SENTRY_DSN.isEmpty()) {
if (path != null)
System.err.println("An error occoured in the path: " + path);
e.printStackTrace();
if (e instanceof ContentNotAvailableException || e instanceof ErrorResponse)
return e;
if ((e instanceof ExtractionException extractionException && extractionException.getMessage().contains("No service can handle the url")))
try {
return new ErrorResponse(new InvalidRequestResponse("Invalid parameter provided, unknown service"), extractionException);
} catch (JsonProcessingException jsonProcessingException) {
throw new RuntimeException(jsonProcessingException);
}
Sentry.captureException(e);
if (Constants.SENTRY_DSN.isEmpty()) {
if (path != null)
System.err.println("An error occoured in the path: " + path);
e.printStackTrace();
}
return e;