Skip to content

Commit

Permalink
[CSE-84] Improve error handling for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
BCook98 committed Dec 18, 2023
1 parent aa913d6 commit e6223f0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
43 changes: 29 additions & 14 deletions .snapshots/TestGenerateWorkatoConnector
Original file line number Diff line number Diff line change
Expand Up @@ -1082,43 +1082,43 @@
when 'api_tasks_v1_tasksservice_createtask'
exclude_keys = ["custom_field"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
post("/v1/tasks").payload(body)
post("/v1/tasks").payload(body).after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
when 'api_tasks_v1_tasksservice_customaction'
exclude_keys = ["custom_field"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
# does a thing
when 'api_tasks_v1_tasksservice_deletetask'
exclude_keys = ["id","custom_field"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
delete("/v1/tasks/#{input.dig(:id)}").params(body)
delete("/v1/tasks/#{input.dig(:id)}").params(body).after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
when 'api_tasks_v1_tasksservice_generatereport'
exclude_keys = ["custom_field"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
post("/v1/tasks/action=generateReport").payload(body)
post("/v1/tasks/action=generateReport").payload(body).after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
when 'api_tasks_v1_tasksservice_gettask'
exclude_keys = ["id","custom_field"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
get("/v1/tasks/#{input.dig(:id)}").params(body)
get("/v1/tasks/#{input.dig(:id)}").params(body).after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
when 'api_tasks_v1_tasksservice_gettaskidsforuser'
exclude_keys = ["user_id","custom_field"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
get("/v1/tasks/#{input.dig(:user_id)}").payload(body)
get("/v1/tasks/#{input.dig(:user_id)}").payload(body).after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
when 'api_tasks_v1_tasksservice_gettaskprivatelybutinworkato'
exclude_keys = ["user_id","custom_field"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
get("/v1/tasks/#{input.dig(:user_id)}").payload(body)
get("/v1/tasks/#{input.dig(:user_id)}").payload(body).after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
when 'api_tasks_v1_tasksservice_updatestatus'
exclude_keys = ["task_id","custom_field"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
post("/v1/tasks/#{input.dig(:task_id)}/status").payload(body)
post("/v1/tasks/#{input.dig(:task_id)}/status").payload(body).after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
when 'api_tasks_v1_tasksservice_updatetask'
exclude_keys = ["id","custom_field"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
put("/v1/tasks/#{input.dig(:id)}").payload(body)
put("/v1/tasks/#{input.dig(:id)}").payload(body).after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
when 'api_tasks_v1_tasksservice_updatetasknested'
exclude_keys = ["task.id","custom_field"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
put("/v1/tasks/#{input.dig(:task, :id)}").payload(body)
put("/v1/tasks/#{input.dig(:task, :id)}").payload(body).after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
end
end,
output_fields: lambda do |object_definitions, connection, config_fields|
Expand Down Expand Up @@ -1198,7 +1198,7 @@
when 'api_tasks_v1_tasksservice_gettask'
exclude_keys = ["id"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
get("/v1/tasks/#{input.dig(:id)}").params(body)
get("/v1/tasks/#{input.dig(:id)}").params(body).after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
end
end,
output_fields: lambda do |object_definitions, connection, config_fields|
Expand Down Expand Up @@ -1260,7 +1260,7 @@
when 'api_tasks_v1_tasksservice_gettask'
exclude_keys = ["id"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
get("/v1/tasks/#{input.dig(:id)}").params(body)
get("/v1/tasks/#{input.dig(:id)}").params(body).after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
end
end,
output_fields: lambda do |object_definitions, connection, config_fields|
Expand Down Expand Up @@ -1328,11 +1328,11 @@
when 'api_tasks_v1_tasksservice_addcomment'
exclude_keys = ["task_id"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
post("/v1/tasks/#{input.dig(:task_id)}/comment").payload(body)
post("/v1/tasks/#{input.dig(:task_id)}/comment").payload(body).after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
when 'api_tasks_v1_tasksservice_updatecomment'
exclude_keys = ["task_id","comment_id"]
body = input.select { |k, v| k != 'action_name' and not exclude_keys.include? k }
put("/v1/tasks/#{input.dig(:task_id)}/comment/#{input.dig(:comment_id)}").payload(body)
put("/v1/tasks/#{input.dig(:task_id)}/comment/#{input.dig(:comment_id)}").payload(body).after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
end
end,
output_fields: lambda do |object_definitions, connection, config_fields|
Expand Down Expand Up @@ -1396,7 +1396,7 @@
"dynamic_api_tasks_v1_tasksservice_listtasks": lambda do
body = {}
qparams = call('encode_array_to_query_params', body)
resp = get("/v1/tasks?#{qparams}")
resp = get("/v1/tasks?#{qparams}").after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end
resp['tasks'].pluck('name', 'id')
end,
"action_name_tasks": lambda do
Expand Down Expand Up @@ -1456,6 +1456,21 @@
# Reusable methods can be called from object_definitions, picklists or actions
# See more at https://docs.workato.com/developing-connectors/sdk/sdk-reference/methods.html
methods: {
# Default after_error_response handler, can be overwritten by defining a new method after this one.
after_error_response: lambda do |code, body, headers, message|
err = [
message,
body,
].join("\n\n")

trace = headers.dig('traceparent') || headers.dig('grpc_metadata_traceparent')
if trace.present?
err = "#{err}\n\nTrace ID: #{trace}"
end

error(err)
end,

"encode_array_to_query_params": lambda do |val|
val.each do |key, value|
if (value.is_a? String) && (value[0] == '[') && (value[-1] == ']')
Expand Down
12 changes: 7 additions & 5 deletions template/action_execute_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

const pbRepeated = "repeated"

const errorHandling = `.after_error_response(/.*/) do |code, body, headers, message| call('after_error_response', code, body, headers, message) end`

// Used to identify parameters in a path e.g. `/users/{used_id}`
var paramMatch = regexp.MustCompile(`({[\.\w]+})`)

Expand Down Expand Up @@ -60,33 +62,33 @@ func (t *WorkatoTemplate) getExecuteCode(service *gendoc.Service, method *gendoc
if rule.Body == "*" {
return schema.ExecCode{
ExcludeFromQuery: params,
Func: fmt.Sprintf(`%s("%s").payload(body)`, mthd, path),
Func: fmt.Sprintf(`%s("%s").payload(body)%s`, mthd, path, errorHandling),
}
}

if rule.Body != "" {
return schema.ExecCode{
ExcludeFromQuery: append(params, rule.Body),
Func: fmt.Sprintf(`%s("%s").payload(input['%s']).params(body)`, mthd, path, rule.Body),
Func: fmt.Sprintf(`%s("%s").payload(input['%s']).params(body)%s`, mthd, path, rule.Body, errorHandling),
}
}

if hasRepeatedType(t.messageMap[method.RequestFullType]) {
return schema.ExecCode{
ExcludeFromQuery: params,
Body: "qparams = call('encode_array_to_query_params', body)",
Func: fmt.Sprintf(`%s("%s?#{qparams}")`, mthd, path),
Func: fmt.Sprintf(`%s("%s?#{qparams}")%s`, mthd, path, errorHandling),
}
} else {
return schema.ExecCode{
ExcludeFromQuery: params,
Func: fmt.Sprintf(`%s("%s").params(body)`, mthd, path),
Func: fmt.Sprintf(`%s("%s").params(body)%s`, mthd, path, errorHandling),
}
}
}
}

return schema.ExecCode{
Func: fmt.Sprintf(`post("/%s/%s").payload(body)`, service.FullName, method.Name),
Func: fmt.Sprintf(`post("/%s/%s").payload(body)%s`, service.FullName, method.Name, errorHandling),
}
}
15 changes: 15 additions & 0 deletions templates/methods.tmpl.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
{{ define "methods" }}
# Default after_error_response handler, can be overwritten by defining a new method after this one.
after_error_response: lambda do |code, body, headers, message|
err = [
message,
body,
].join("\n\n")

trace = headers.dig('traceparent') || headers.dig('grpc_metadata_traceparent')
if trace.present?
err = "#{err}\n\nTrace ID: #{trace}"
end

error(err)
end,

"encode_array_to_query_params": lambda do |val|
val.each do |key, value|
if (value.is_a? String) && (value[0] == '[') && (value[-1] == ']')
Expand Down

0 comments on commit e6223f0

Please sign in to comment.