Skip to content

Commit

Permalink
improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
klarysz committed Aug 15, 2022
1 parent c167c06 commit 143230a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 37 deletions.
2 changes: 2 additions & 0 deletions cmd/ast/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func (m *ViewMeta) addParameter(param *Parameter) {
parameter.Cardinality = view.Many
}

parameter.Repeated = parameter.Repeated || param.Repeated

parameter.Required = boolPtr((parameter.Required != nil && *parameter.Required) || (param.Required != nil && *param.Required))
if parameter.Assumed {
parameter.DataType = param.DataType
Expand Down
22 changes: 4 additions & 18 deletions cmd/ast/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ func buildViewMetaInExecSQLMode(SQL string, view *ViewMeta, variables map[string
lcSQL := strings.ToLower(SQL)
boundary := getStatementBoundary(lcSQL)

SQLExec := "#set($sIndex = 0)\n"
i := 0
isLast := i+1 == len(boundary)
SQLExec := ""
if len(boundary) > 0 {
offset := boundary[0]
limit := len(SQL)
Expand All @@ -28,13 +26,10 @@ func buildViewMetaInExecSQLMode(SQL string, view *ViewMeta, variables map[string
if err != nil {
return err
}

SQLExec += normalizedSQL
if !isLast {
SQLExec += "\n#set($sIndex = $sIndex+1)\n"
}

for i := 1; i < len(boundary); i++ {
isLast = i+1 == len(boundary)
offset = boundary[i]
limit = len(SQL)
if i+1 < len(boundary) {
Expand All @@ -45,9 +40,6 @@ func buildViewMetaInExecSQLMode(SQL string, view *ViewMeta, variables map[string
return err
}
SQLExec += normalizedSQL
if !isLast {
SQLExec += "\n#set($sIndex = $sIndex+1)\n"
}
}
}
view.Source = SQLExec
Expand Down Expand Up @@ -144,13 +136,7 @@ func normalizeAndExtractUpdateWhere(stmt *update.Statement, view *ViewMeta, SQLE
view.addParameter(&Parameter{Id: paramName, Name: paramName, Repeated: true, Typer: &ColumnType{
ColumnName: criterion.X,
}})
SQLExec = strings.Replace(SQLExec, y, fmt.Sprintf(`
#set($coma = "")
#foreach($Key in %v)
$coma $criteria.Add($sIndex, $Key)
#set($coma = ",")
#end
`, sanitizeUnsafeParameter(y)), 1)

case "=":
paramName := y[1:]
required := true
Expand Down Expand Up @@ -189,7 +175,7 @@ func normalizeAndExtractUpdateSet(stmt *update.Statement, view *ViewMeta, rawSQL

func sanitizeUnsafeExpr(paramName string) string {
paramName = sanitizeUnsafeParameter(paramName)
return fmt.Sprintf("$criteria.Add($sIndex,%v)", paramName)
return fmt.Sprintf("$criteria.AppendBinding(%v)", paramName)
}

func sanitizeUnsafeParameter(paramName string) string {
Expand Down
13 changes: 8 additions & 5 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,17 @@ func (r *Router) normalizeErr(err error, statusCode int) (int, error) {
switch actual := err.(type) {
case *Errors:
for _, anError := range actual.Errors {
switch actual := anError.Err.(type) {
switch childErr := anError.Err.(type) {
case validator.ValidationErrors:
anError.Object = NewParamErrors(actual)
anError.Object = NewParamErrors(childErr)
anError.Message = childErr.Error()
case *Errors:
_, anError.Object = r.normalizeErr(anError.Err, statusCode)
case *JSONError:
anError.Object = actual
anError.Object = childErr
default:
anError.Message = anError.Error()
}

anError.Message = anError.Error()
}

if actual.status != 0 {
Expand Down
17 changes: 3 additions & 14 deletions router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func TestRouter(t *testing.T) {
afterInsertExpected: `[{"Id":1,"Timestamp":"2019-03-11T02:20:33Z","EventTypeId":2,"Quantity":40,"UserId":1},{"Id":10,"Timestamp":"2019-03-15T12:07:33Z","EventTypeId":11,"Quantity":40,"UserId":2},{"Id":103,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":40,"UserId":3}]`,
},
{
description: "executor with param slice",
description: "executor with param slice | error",
resourceURI: "030_param_slice",
uri: "/api/events",
method: http.MethodPost,
Expand All @@ -636,7 +636,7 @@ func TestRouter(t *testing.T) {
expected: `{"Message":"invalid template due to: invalid status"}`,
},
{
description: "multiple execution statements",
description: "multiple execution statements | multiple execs",
resourceURI: "031_multiple_execs",
uri: "/api/events",
method: http.MethodPost,
Expand All @@ -646,17 +646,6 @@ func TestRouter(t *testing.T) {
requestBody: `{"ID": [1,10,103], "Quantity": 40}`,
afterInsertExpected: `[{"Id":1,"Timestamp":"2019-03-11T02:20:33Z","EventTypeId":2,"Quantity":40,"UserId":1},{"Id":10,"Timestamp":"2019-03-15T12:07:33Z","EventTypeId":11,"Quantity":40,"UserId":2},{"Id":103,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":40,"UserId":3}]`,
},
{
description: "multiple execution statements",
resourceURI: "031_multiple_execs",
uri: "/api/events",
method: http.MethodPost,
visitors: map[string]codec.LifecycleVisitor{},
afterInsertUri: "/api/events?_criteria=Quantity=40",
afterInsertMethod: http.MethodGet,
afterInsertExpected: `[{"Id":1,"Timestamp":"2019-03-11T02:20:33Z","EventTypeId":2,"Quantity":40,"UserId":1},{"Id":10,"Timestamp":"2019-03-15T12:07:33Z","EventTypeId":11,"Quantity":40,"UserId":2},{"Id":103,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":40,"UserId":3}]`,
requestBody: `{"ID": [1,10,103], "Quantity": 40}`,
},
{
description: "extract values from Request Body",
resourceURI: "032_request_body",
Expand All @@ -673,7 +662,7 @@ func TestRouter(t *testing.T) {
method: http.MethodPost,
visitors: map[string]codec.LifecycleVisitor{},
requestBody: `{"ID": [1,10,103], "Quantity": 0}`,
expected: `{"Errors":[{"View":"events","Param":"Data","Message":"[{\"Id\":1,\"Status\":false},{\"Id\":10,\"Status\":false},{\"Id\":103,\"Status\":false}]","Object":[{"Id":1,"Status":false},{"Id":10,"Status":false},{"Id":103,"Status":false}]}]}`,
expected: `{"Errors":[{"View":"events","Param":"Data","Object":[{"Id":1,"Status":false},{"Id":10,"Status":false},{"Id":103,"Status":false}]}]}`,
},
{
description: "executor with param slice",
Expand Down

0 comments on commit 143230a

Please sign in to comment.