Skip to content

Commit

Permalink
feat: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
astappiev committed Sep 17, 2024
1 parent 0a7d4a3 commit a9d5576
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static Instant parse(String str) {
try {
return Instant.from(PARSE_FORMAT.parse(str));
} catch (DateTimeParseException e) {
throw new ConnectorException("Unable to parse date: " + str, e);
throw new ConnectorException("Unable to parse date in a string `" + str + "`", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package de.l3s.interweb.server.config;

import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.core.Response;

import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;

public class ExceptionMappers {
@ServerExceptionMapper
public RestResponse<HttpError> mapException(Exception x) {
return RestResponse.status(Response.Status.INTERNAL_SERVER_ERROR, HttpError.of(x));
}

@ServerExceptionMapper
public RestResponse<HttpError> mapException(NotFoundException x) {
return RestResponse.status(Response.Status.NOT_FOUND, HttpError.of(x));
}

@ServerExceptionMapper
public RestResponse<HttpError> mapException(BadRequestException x) {
return RestResponse.status(Response.Status.BAD_REQUEST, HttpError.of(x));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Uni<CompletionsResults> completions(CompletionsQuery query) {
return completions(query, model, chatConnector);
}

return Uni.createFrom().failure(new ConnectorException("Model doesn't support chat: " + query.getModel()));
return Uni.createFrom().failure(new ConnectorException("Model `" + query.getModel() + "` is not a chat model"));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private DescribeConnector getConnector(Set<String> services) {
String val = services.iterator().next();
DescribeConnector connector = this.services.get(val.toLowerCase(Locale.ROOT));
if (connector == null) {
throw new ConnectorException("Unknown service: " + val);
throw new ConnectorException("Service `" + val + "` is unknown");
}
return connector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ public Uni<List<Model>> getModels() {
public Uni<Model> getModel(String modelId) {
return getModels().map(models -> models.stream()
.filter(model -> model.getId().equalsIgnoreCase(modelId.toLowerCase(Locale.ROOT)))
.findFirst().orElseThrow(() -> new NotFoundException("Model not found: " + modelId)));
.findFirst().orElseThrow(() -> new NotFoundException("Model `" + modelId + "` not found")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private Collection<SearchConnector> getConnectors(Set<String> services) {
return services.stream().map(val -> {
SearchConnector connector = this.providers.get(val.toLowerCase(Locale.ROOT));
if (connector == null) {
throw new ConnectorException("Unknown service: " + val);
throw new ConnectorException("Service `" + val + "` is unknown");
}
return connector;
}).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private Collection<SuggestConnector> getConnectors(Set<String> services) {
return services.stream().map(val -> {
SuggestConnector connector = this.services.get(val.toLowerCase(Locale.ROOT));
if (connector == null) {
throw new ConnectorException("Unknown service: " + val);
throw new ConnectorException("Service `" + val + "` is unknown");
}
return connector;
}).toList();
Expand All @@ -52,7 +52,7 @@ public void validateServices(Set<String> services) {
if (services != null && !services.isEmpty()) {
for (String service : services) {
if (!this.services.containsKey(service)) {
throw new ValidationException("Service unknown.");
throw new ValidationException("Service unknown");
}
}
}
Expand Down

0 comments on commit a9d5576

Please sign in to comment.