Skip to content

Commit

Permalink
Return null for an invalid element when fetching with a query (bulk i…
Browse files Browse the repository at this point in the history
…ncluded) instead of aborting the whole request and returning a text error.

Remove the "[name] is not a real [type]" message.
  • Loading branch information
MrToirol committed Jul 8, 2024
1 parent 0c2e49c commit f48779e
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class NationsEndpoint {

public String lookup(String query) {
return EndpointUtils.lookup(query, EndpointUtils::getNationOrNull, "is not a real nation");
return EndpointUtils.lookup(query, EndpointUtils::getNationOrNull);
}

public static JsonObject getNationObject(Nation nation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public PlayersEndpoint(Economy economy) {
}

public String lookup(String query) {
return EndpointUtils.lookup(query, EndpointUtils::getResidentOrNull, "is not a real player");
return EndpointUtils.lookup(query, EndpointUtils::getResidentOrNull);
}

public static JsonObject getPlayerObject(Resident resident) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class QuartersEndpoint {

public String lookup(String query) {
return EndpointUtils.lookup(query, EndpointUtils::getQuarterOrNull, "is not a real quarter");
return EndpointUtils.lookup(query, EndpointUtils::getQuarterOrNull);
}

public static JsonObject getQuarterObject(Quarter quarter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class TownsEndpoint {

public String lookup(String query) {
return EndpointUtils.lookup(query, EndpointUtils::getTownOrNull, "is not a real town");
return EndpointUtils.lookup(query, EndpointUtils::getTownOrNull);
}

public static JsonObject getTownObject(Town town) {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/earthmc/emcapi/util/EndpointUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyPermission;
import io.javalin.http.NotFoundResponse;
import net.earthmc.emcapi.EMCAPI;
import net.earthmc.emcapi.endpoint.NationsEndpoint;
import net.earthmc.emcapi.endpoint.PlayersEndpoint;
Expand All @@ -28,7 +27,7 @@

public class EndpointUtils {

public static String lookup(String query, Function<String, ?> getObject, String notFoundMessage) {
public static String lookup(String query, Function<String, ?> getObject) {
String[] split = query.split(",");
JsonArray jsonArray = new JsonArray();

Expand All @@ -47,7 +46,7 @@ public static String lookup(String query, Function<String, ?> getObject, String
jsonArray.add(QuartersEndpoint.getQuarterObject((Quarter) object));
}
} else {
throw new NotFoundResponse(name + " " + notFoundMessage);
jsonArray.add((JsonObject) null);
}
}

Expand Down

0 comments on commit f48779e

Please sign in to comment.