diff --git a/src/main/java/com/homihq/db2rest/exception/GenericDataAccessException.java b/src/main/java/com/homihq/db2rest/exception/GenericDataAccessException.java new file mode 100644 index 00000000..755052f2 --- /dev/null +++ b/src/main/java/com/homihq/db2rest/exception/GenericDataAccessException.java @@ -0,0 +1,25 @@ +package com.homihq.db2rest.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ProblemDetail; +import org.springframework.web.ErrorResponseException; + +import java.net.URI; +import java.time.Instant; + +public class GenericDataAccessException extends ErrorResponseException { + + + public GenericDataAccessException(String message) { + super(HttpStatus.BAD_REQUEST, asProblemDetail(message), null); + } + + private static ProblemDetail asProblemDetail(String message) { + ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, message); + problemDetail.setTitle("Generic Data Access Error"); + problemDetail.setType(URI.create("https://github.com/kdhrubo/db2rest/generic-error")); + problemDetail.setProperty("errorCategory", "Data-access-error"); + problemDetail.setProperty("timestamp", Instant.now()); + return problemDetail; + } +} diff --git a/src/main/java/com/homihq/db2rest/rest/create/CreateService.java b/src/main/java/com/homihq/db2rest/rest/create/CreateService.java index 77a640cf..b6cc4b11 100644 --- a/src/main/java/com/homihq/db2rest/rest/create/CreateService.java +++ b/src/main/java/com/homihq/db2rest/rest/create/CreateService.java @@ -1,6 +1,7 @@ package com.homihq.db2rest.rest.create; import com.homihq.db2rest.config.Db2RestConfigProperties; +import com.homihq.db2rest.exception.GenericDataAccessException; import com.homihq.db2rest.mybatis.DB2RestRenderingStrategy; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -11,6 +12,7 @@ import org.mybatis.dynamic.sql.insert.render.BatchInsert; import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider; import org.mybatis.dynamic.sql.render.RenderingStrategies; +import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils; @@ -43,14 +45,19 @@ public int save(String schemaName, String tableName, Map data) { generalInsertDSL.set(table.column(key)).toValue(data.get(key)); } - GeneralInsertStatementProvider insertStatement = generalInsertDSL.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER); log.debug("SQL - {}", insertStatement.getInsertStatement()); log.debug("SQL - row - {}", insertStatement.getParameters()); - int rows = namedParameterJdbcTemplate.update(insertStatement.getInsertStatement(), insertStatement.getParameters()); + int rows = 0; + try { + rows = namedParameterJdbcTemplate.update(insertStatement.getInsertStatement(), insertStatement.getParameters()); + } + catch(DataAccessException e) { + throw new GenericDataAccessException(e.getMessage()); + } log.debug("Inserted - {} row(s)", rows); return rows; diff --git a/src/test/java/com/homihq/db2rest/rest/PgCreateControllerTest.java b/src/test/java/com/homihq/db2rest/rest/PgCreateControllerTest.java index 13e08277..1054b096 100644 --- a/src/test/java/com/homihq/db2rest/rest/PgCreateControllerTest.java +++ b/src/test/java/com/homihq/db2rest/rest/PgCreateControllerTest.java @@ -48,4 +48,35 @@ void create() throws Exception { .andDo(document("create-a-film")); } + + @Test + @DisplayName("Create a film with error.") + void createError() throws Exception { + + var json = """ + { + "title" : "Dunki", + "description" : "Film about illegal immigration" , + "release_year" : 2023, + "language_id" : 1, + "original_language_id" : null, + "rental_duration" : 6, + "rental_rate" : 0.99 , + "length" : 150, + "replacement_cost" : 20.99 , + "rating" : "PG-13" , + "special_features" : "Commentaries", + "country" : "USA" + } + """; + + + mockMvc.perform(post("/film").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8") + .header("Content-Profile", "public") + .content(json).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()) + .andDo(print()) + .andDo(document("create-a-film-error")); + + } }