Skip to content

Commit

Permalink
Exception handling for procedure or function call
Browse files Browse the repository at this point in the history
  • Loading branch information
souravroy committed Jan 10, 2024
1 parent baa7178 commit 0e98b2d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
29 changes: 29 additions & 0 deletions src/main/java/com/homihq/db2rest/exception/RpcException.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.Map;

@RestController
Expand All @@ -18,7 +17,8 @@ public class FunctionController {

@PostMapping("/{funcName}")
public ResponseEntity<Map<String, Object>> execute(@PathVariable String funcName,
@RequestBody Map<String,Object> inParams) throws SQLException {
@RequestBody Map<String,Object> inParams) {
log.debug("Execute function {} with IN params {}", funcName, inParams.entrySet());
return ResponseEntity.ok(functionService.execute(funcName, inParams));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.Map;

@RestController
Expand All @@ -18,7 +17,8 @@ public class ProcedureController {

@PostMapping("/{procName}")
public ResponseEntity<Map<String, Object>> execute(@PathVariable String procName,
@RequestBody Map<String,Object> inParams) throws SQLException {
@RequestBody Map<String,Object> inParams) {
log.debug("Execute stored procedure {} with IN params {}", procName, inParams.entrySet());
return ResponseEntity.ok(procedureService.execute(procName, inParams));
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/homihq/db2rest/rest/rpc/SubRoutine.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +20,11 @@ Map<String, Object> execute(String subRoutineName, Map<String, Object> 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);
Expand Down

0 comments on commit 0e98b2d

Please sign in to comment.