Skip to content

Commit

Permalink
now catching UnableToExecuteStatementException
Browse files Browse the repository at this point in the history
  • Loading branch information
jmilkiewicz committed Nov 20, 2023
1 parent 0d2a583 commit cf1d909
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 32 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.exacaster.lighter.rest.exceptions;

import com.exacaster.lighter.application.sessions.exceptions.SessionAlreadyExistsException;
import com.exacaster.lighter.storage.ApplicationAlreadyExistsException;
import io.micronaut.context.annotation.Requires;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
Expand All @@ -17,23 +17,23 @@

@Produces
@Singleton
@Requires(classes = SessionAlreadyExistsException.class)
public class SessionAlreadyExistsExceptionHandler implements ExceptionHandler<SessionAlreadyExistsException, HttpResponse<?>> {
@Requires(classes = ApplicationAlreadyExistsException.class)
public class ApplicationAlreadyExistsExceptionHandler implements ExceptionHandler<ApplicationAlreadyExistsException, HttpResponse<?>> {

private final ErrorResponseProcessor<?> responseProcessor;

@Inject
public SessionAlreadyExistsExceptionHandler(ErrorResponseProcessor<?> responseProcessor) {
public ApplicationAlreadyExistsExceptionHandler(ErrorResponseProcessor<?> responseProcessor) {
this.responseProcessor = responseProcessor;
}

@Override
public HttpResponse<?> handle(HttpRequest request, SessionAlreadyExistsException exception) {
public HttpResponse<?> handle(HttpRequest request, ApplicationAlreadyExistsException exception) {
final ErrorContext.Builder contextBuilder = ErrorContext.builder(request).cause(exception);
MutableHttpResponse<?> response = HttpResponse.status(HttpStatus.CONFLICT);

return responseProcessor.processResponse(contextBuilder
.error(new DetailedError(exception.getMessage(), Map.of("sessionId", exception.getSessionId())))
.error(new DetailedError(exception.getMessage(), Map.of("sessionId", exception.getApplicationId())))
.build(), response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.exacaster.lighter.storage;

public class ApplicationAlreadyExistsException extends RuntimeException {
private final String applicationId;

public ApplicationAlreadyExistsException(String applicationId) {
super("Application already exists");
this.applicationId = applicationId;
}

public String getApplicationId() {
return applicationId;
}

@Override
public String toString() {
return "ApplicationAlreadyExistsException{" +
"applicationId='" + applicationId + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface ApplicationStorage {

Application saveApplication(Application application);

Application insertApplication(Application application);
Application insertApplication(Application application) throws ApplicationAlreadyExistsException;

List<Application> findApplicationsByStates(ApplicationType type, List<ApplicationState> states, SortOrder order,
Integer from, Integer size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.exacaster.lighter.application.ApplicationType;
import com.exacaster.lighter.application.SubmitParams;
import com.exacaster.lighter.storage.ApplicationStorage;
import com.exacaster.lighter.storage.ApplicationAlreadyExistsException;
import com.exacaster.lighter.storage.SortOrder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -15,6 +16,7 @@
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.core.statement.UnableToExecuteStatementException;
import org.slf4j.Logger;

import javax.sql.DataSource;
Expand Down Expand Up @@ -92,10 +94,16 @@ public Application saveApplication(Application application) {
@Override
@Transactional
public Application insertApplication(Application application) {
return jdbi.withHandle(handle -> {
insert(handle, application);
return application;
});
try {
return jdbi.withHandle(handle -> {
insert(handle, application);
return application;
});
} catch (UnableToExecuteStatementException e) {
//can not make it better till https://github.com/jdbi/jdbi/issues/566 is implemented
throw new ApplicationAlreadyExistsException(application.getId());
}

}

private static int update(Handle handle, Application application) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.exacaster.lighter.storage.jdbc
import com.exacaster.lighter.application.ApplicationBuilder
import com.exacaster.lighter.application.ApplicationState
import com.exacaster.lighter.application.ApplicationType
import com.exacaster.lighter.storage.ApplicationAlreadyExistsException
import com.exacaster.lighter.storage.SortOrder
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
Expand Down Expand Up @@ -91,4 +92,16 @@ class JdbcApplicationStorageTest extends Specification {
// then: "fetching apps ignores soft deleted ones"
// storage.findAllPermanentSessions().size() == 1
// }


def "insert"() {
given:
def savedSession = storage.saveApplication(newPermanentSession())

when: "inserting a session with id that already exists"
storage.insertApplication(savedSession)

then:
thrown ApplicationAlreadyExistsException
}
}

0 comments on commit cf1d909

Please sign in to comment.