From 0e98b2dda00f2dfe27bcbdf9ac63603ea5862043 Mon Sep 17 00:00:00 2001 From: Sourav Roy Date: Wed, 10 Jan 2024 20:29:13 +0000 Subject: [PATCH] Exception handling for procedure or function call --- .../db2rest/exception/RpcException.java | 29 +++++++++++++++++++ .../db2rest/rest/rpc/FunctionController.java | 4 +-- .../db2rest/rest/rpc/ProcedureController.java | 4 +-- .../homihq/db2rest/rest/rpc/SubRoutine.java | 8 ++++- 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/homihq/db2rest/exception/RpcException.java diff --git a/src/main/java/com/homihq/db2rest/exception/RpcException.java b/src/main/java/com/homihq/db2rest/exception/RpcException.java new file mode 100644 index 00000000..6cf0c636 --- /dev/null +++ b/src/main/java/com/homihq/db2rest/exception/RpcException.java @@ -0,0 +1,29 @@ +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; +import java.util.Map; + +public class RpcException extends ErrorResponseException { + + public RpcException(String subRoutineName, Map inParams) { + super(HttpStatus.BAD_REQUEST, + asProblemDetail("Procedure/Function name: " + + subRoutineName + ", IN parameters: " + + inParams.entrySet()), null); + } + + private static ProblemDetail asProblemDetail(String message) { + ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, message); + problemDetail.setTitle("Invalid Procedure/Function name or IN parameter mismatch"); + problemDetail.setDetail(message); + problemDetail.setType(URI.create("https://github.com/kdhrubo/db2rest/invalid-subroutine-request")); + problemDetail.setProperty("errorCategory", "Invalid-SubRoutine-Request"); + problemDetail.setProperty("timestamp", Instant.now()); + return problemDetail; + } +} diff --git a/src/main/java/com/homihq/db2rest/rest/rpc/FunctionController.java b/src/main/java/com/homihq/db2rest/rest/rpc/FunctionController.java index 23018cc6..a3ceb8e9 100644 --- a/src/main/java/com/homihq/db2rest/rest/rpc/FunctionController.java +++ b/src/main/java/com/homihq/db2rest/rest/rpc/FunctionController.java @@ -5,7 +5,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.sql.SQLException; import java.util.Map; @RestController @@ -18,7 +17,8 @@ public class FunctionController { @PostMapping("/{funcName}") public ResponseEntity> execute(@PathVariable String funcName, - @RequestBody Map inParams) throws SQLException { + @RequestBody Map inParams) { + log.debug("Execute function {} with IN params {}", funcName, inParams.entrySet()); return ResponseEntity.ok(functionService.execute(funcName, inParams)); } } diff --git a/src/main/java/com/homihq/db2rest/rest/rpc/ProcedureController.java b/src/main/java/com/homihq/db2rest/rest/rpc/ProcedureController.java index c4e22e21..cd0eb785 100644 --- a/src/main/java/com/homihq/db2rest/rest/rpc/ProcedureController.java +++ b/src/main/java/com/homihq/db2rest/rest/rpc/ProcedureController.java @@ -5,7 +5,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.sql.SQLException; import java.util.Map; @RestController @@ -18,7 +17,8 @@ public class ProcedureController { @PostMapping("/{procName}") public ResponseEntity> execute(@PathVariable String procName, - @RequestBody Map inParams) throws SQLException { + @RequestBody Map inParams) { + log.debug("Execute stored procedure {} with IN params {}", procName, inParams.entrySet()); return ResponseEntity.ok(procedureService.execute(procName, inParams)); } } diff --git a/src/main/java/com/homihq/db2rest/rest/rpc/SubRoutine.java b/src/main/java/com/homihq/db2rest/rest/rpc/SubRoutine.java index 667612e1..1db449d1 100644 --- a/src/main/java/com/homihq/db2rest/rest/rpc/SubRoutine.java +++ b/src/main/java/com/homihq/db2rest/rest/rpc/SubRoutine.java @@ -1,7 +1,9 @@ package com.homihq.db2rest.rest.rpc; +import com.homihq.db2rest.exception.RpcException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.SqlParameterSource; @@ -18,7 +20,11 @@ Map execute(String subRoutineName, Map inParams) jdbcTemplate.setResultsMapCaseInsensitive(true); SqlParameterSource in = new MapSqlParameterSource() .addValues(inParams); - return getSimpleJdbcCall(subRoutineName).execute(in); + try { + return getSimpleJdbcCall(subRoutineName).execute(in); + } catch (InvalidDataAccessApiUsageException ex) { + throw new RpcException(subRoutineName, inParams); + } } abstract SimpleJdbcCall getSimpleJdbcCall(String subRoutineName);