Add class for common error messages

This commit is contained in:
0xb00bface 2021-12-24 16:18:22 +01:00
parent 38b898f405
commit e30a658f3d
12 changed files with 35 additions and 18 deletions

View File

@ -1,5 +1,6 @@
package ctbrec.ui.news;
import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL;
import static ctbrec.io.HttpConstants.*;
import java.io.IOException;
@ -52,7 +53,7 @@ public class NewsTab extends Tab implements TabSelectionListener {
.build();
try (var response = CamrecApplication.httpClient.execute(request)) {
if (response.isSuccessful()) {
var body = Objects.requireNonNull(response.body(), "HTTP response body is null").string();
var body = Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string();
if (body.startsWith("[")) {
onSuccess(body);
} else if (body.startsWith("{")) {

View File

@ -19,6 +19,7 @@ import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL;
import static ctbrec.Model.State.OFFLINE;
import static ctbrec.Model.State.ONLINE;
@ -66,7 +67,7 @@ public class CamsodaUpdateService extends PaginatedScheduledService {
var request = new Request.Builder().url(url).build();
try (var response = camsoda.getHttpClient().execute(request)) {
if (response.isSuccessful()) {
return parseModels(Objects.requireNonNull(response.body(), "HTTP response body is null").string());
return parseModels(Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string());
} else {
throw new HttpException(response.code(), response.message());
}

View File

@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.*;
import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL;
import static ctbrec.io.HttpConstants.*;
public class SecretFriendsUpdateService extends PaginatedScheduledService {
@ -56,7 +57,7 @@ public class SecretFriendsUpdateService extends PaginatedScheduledService {
.build();
try (var response = site.getHttpClient().execute(request)) {
if (response.isSuccessful()) {
return parseModels(Objects.requireNonNull(response.body(), "HTTP response body is null").string());
return parseModels(Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string());
} else {
throw new HttpException(response.code(), response.message());
}

View File

@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.*;
import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL;
import static ctbrec.Model.State.OFFLINE;
import static ctbrec.Model.State.ONLINE;
import static ctbrec.io.HttpConstants.*;
@ -62,7 +63,7 @@ public class StreamateFollowedService extends PaginatedScheduledService {
.build();
try (var response = streamate.getHttpClient().execute(request)) {
if (response.isSuccessful()) {
return parseModels(Objects.requireNonNull(response.body(), "HTTP response body is null").string());
return parseModels(Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string());
} else {
throw new HttpException(response.code(), response.message());
}

View File

@ -17,6 +17,7 @@ import java.util.List;
import java.util.Objects;
import java.util.UUID;
import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL;
import static ctbrec.Model.State.*;
import static ctbrec.io.HttpConstants.ORIGIN;
@ -56,7 +57,7 @@ public class StreamateUpdateService extends PaginatedScheduledService {
.build();
try (var response = httpClient.execute(request)) {
if (response.isSuccessful()) {
return parseModels(Objects.requireNonNull(response.body(), "HTTP response body is null").string());
return parseModels(Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string());
} else {
throw new HttpException(response.code(), response.message());
}

View File

@ -0,0 +1,8 @@
package ctbrec;
public class ErrorMessages {
private ErrorMessages() {}
public static final String HTTP_RESPONSE_BODY_IS_NULL = "HTTP response body is null";
}

View File

@ -1,5 +1,6 @@
package ctbrec.recorder.download.hls;
import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL;
import static ctbrec.recorder.download.hls.AbstractHlsDownload.*;
import java.io.FileNotFoundException;
@ -77,7 +78,7 @@ public class SegmentDownload implements Callable<SegmentDownload> {
private void handleResponse(Response response) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IOException {
if (response.isSuccessful()) {
InputStream in = Objects.requireNonNull(response.body(), "HTTP response body is null").byteStream();
InputStream in = Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).byteStream();
if (playlist.encrypted) {
in = new Crypto(playlist.encryptionKeyUrl, client).wrap(in);
}

View File

@ -29,6 +29,7 @@ import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL;
import static ctbrec.Model.State.*;
import static ctbrec.io.HttpConstants.*;
import static java.nio.charset.StandardCharsets.UTF_8;
@ -216,7 +217,7 @@ public class CherryTvModel extends AbstractModel {
LOG.debug("Sending follow request for model {} with ID {}", getName(), getId());
try (Response response = getSite().getHttpClient().execute(request)) {
if (response.isSuccessful()) {
String responseBody = Objects.requireNonNull(response.body(), "HTTP response body is null").string();
String responseBody = Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string();
LOG.debug(responseBody);
JSONObject resp = new JSONObject(responseBody);
if (resp.has("data") && !resp.isNull("data")) {

View File

@ -20,6 +20,7 @@ import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL;
import static ctbrec.io.HttpConstants.USER_AGENT;
public class SecretFriends extends AbstractSite {
@ -111,7 +112,7 @@ public class SecretFriends extends AbstractSite {
.build();
try (Response response = getHttpClient().execute(req)) {
if (response.isSuccessful()) {
String body = Objects.requireNonNull(response.body(), "HTTP response body is null").string();
String body = Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string();
List<Model> models = new ArrayList<>();
Elements modelDivs = HtmlParser.getTags(body, "div[class~=model-wrapper]");
LOG.debug("Found {} models", modelDivs.size());

View File

@ -22,6 +22,7 @@ import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL;
import static ctbrec.Model.State.ONLINE;
import static ctbrec.io.HttpConstants.*;
@ -46,7 +47,7 @@ public class SecretFriendsModel extends AbstractModel {
.build();
try (Response response = site.getHttpClient().execute(req)) {
if (response.isSuccessful()) {
String body = Objects.requireNonNull(response.body(), "HTTP response body is null").string();
String body = Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string();
Element wrapper = HtmlParser.getTag(body, "div[class~=model-wrapper]");
SecretFriendsModel parsedModel = SecretFriendsModelParser.parse((SecretFriends) getSite(), wrapper);
setName(parsedModel.getName());
@ -118,7 +119,7 @@ public class SecretFriendsModel extends AbstractModel {
.build();
try (Response response = site.getHttpClient().execute(req)) {
if (response.isSuccessful()) {
String body = Objects.requireNonNull(response.body(), "HTTP response body is null").string();
String body = Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string();
return new JSONObject(body);
} else {
throw new HttpException(response.code(), response.message());
@ -137,7 +138,7 @@ public class SecretFriendsModel extends AbstractModel {
.build();
try (Response response = site.getHttpClient().execute(req)) {
if (response.isSuccessful()) {
return Objects.requireNonNull(response.body(), "HTTP response body is null").string();
return Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string();
} else {
throw new HttpException(response.code(), response.message());
}

View File

@ -13,6 +13,7 @@ import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL;
import static ctbrec.io.HttpConstants.*;
public class StreamateHttpClient extends HttpClient {
@ -21,7 +22,6 @@ public class StreamateHttpClient extends HttpClient {
private static final Logger LOG = LoggerFactory.getLogger(StreamateHttpClient.class);
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public static final String HTTP_BODY_IS_NULL = "HTTP response body is null";
private Long userId;
private String saKey = "";
@ -58,7 +58,7 @@ public class StreamateHttpClient extends HttpClient {
.build();
try (Response resp = execute(req)) {
if (resp.code() == 200) {
String body = Objects.requireNonNull(resp.body(), HTTP_BODY_IS_NULL).string();
String body = Objects.requireNonNull(resp.body(), HTTP_RESPONSE_BODY_IS_NULL).string();
LOG.info("Initial request was fine, Extracting XSRF token");
Matcher m = Pattern.compile("\"xsrfToken\":\"(.*?)\"", Pattern.DOTALL).matcher(body);
if (m.find()) {
@ -114,7 +114,7 @@ public class StreamateHttpClient extends HttpClient {
.post(body)
.build();
try (Response response = client.newCall(login).execute()) {
String content = Objects.requireNonNull(response.body(), HTTP_BODY_IS_NULL).string();
String content = Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string();
if (response.isSuccessful()) {
JSONObject json = new JSONObject(content);
loggedIn = json.has(SAKEY_KEY);
@ -155,7 +155,7 @@ public class StreamateHttpClient extends HttpClient {
Request request = newRequestBuilder().url(url).build();
try (Response response = execute(request)) {
if (response.isSuccessful()) {
String content = Objects.requireNonNull(response.body(), HTTP_BODY_IS_NULL).string();
String content = Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string();
JSONObject json = new JSONObject(content);
return json.optString("status").equals("SM_OK");
} else {

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL;
import static ctbrec.Model.State.*;
import static ctbrec.io.HttpConstants.*;
import static ctbrec.sites.streamate.StreamateHttpClient.JSON;
@ -33,7 +34,6 @@ public class StreamateModel extends AbstractModel {
private static final Logger LOG = LoggerFactory.getLogger(StreamateModel.class);
private static final Long MODEL_ID_UNDEFINED = -1L;
private static final String HTTP_BODY_IS_NULL = "HTTP response body is null";
private boolean online = false;
private final transient List<StreamSource> streamSources = new ArrayList<>();
@ -87,7 +87,7 @@ public class StreamateModel extends AbstractModel {
.build();
try (Response response = site.getHttpClient().execute(req)) {
if (response.isSuccessful()) {
String body = Objects.requireNonNull(response.body(), HTTP_BODY_IS_NULL).string();
String body = Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string();
JSONObject json = new JSONObject(body);
JSONObject formats = json.getJSONObject("formats");
JSONObject hls = formats.getJSONObject("mp4-hls");
@ -197,7 +197,7 @@ public class StreamateModel extends AbstractModel {
.post(body)
.build();
try (Response response = getSite().getHttpClient().execute(request)) {
String content = Objects.requireNonNull(response.body(), HTTP_BODY_IS_NULL).string();
String content = Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string();
if (response.isSuccessful()) {
JSONObject json = new JSONObject(content);
return json.optBoolean("success");