Skip to content

Commit 7e297ef

Browse files
committed
WIP: think we want to move away from parsing at all inside workflow
also, team not thrilled with having the wrap stuff everywhere. so need to come back
1 parent 3db3ae8 commit 7e297ef

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

lib/floe/workflow.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,14 @@ def wait(workflows, timeout: nil, &block)
7979

8080
attr_reader :comment, :context
8181

82+
# @param [Json String|Hash|Array] payload
83+
# @param [Context|Hash] context
84+
# @param [Hash|String|nil] credentials injects into credentials
85+
# @param [String|nil] name (defaults to State Machine)
8286
def initialize(payload, context = nil, credentials = nil, name = nil)
83-
payload = JSON.parse(payload) if payload.kind_of?(String)
84-
credentials = JSON.parse(credentials) if credentials.kind_of?(String)
85-
context = Context.new(context) unless context.kind_of?(Context)
87+
payload = self.class.safe_parse("Payload", payload, error: InvalidWorkflowError) if payload.kind_of?(String)
88+
credentials = self.class.safe_parse("Credentials", credentials) if credentials.kind_of?(String)
89+
context = Context.new(context) unless context.kind_of?(Context)
8690

8791
# backwards compatibility
8892
# caller should really put credentials into context and not pass that variable
@@ -194,5 +198,11 @@ def step!
194198
def end_workflow!
195199
context.execution["EndTime"] = context.state["FinishedTime"]
196200
end
201+
202+
def self.safe_parse(field, string, error: Floe::InvalidExecutionInput)
203+
JSON.parse(string)
204+
rescue JSON::ParserError => err
205+
raise error, "Invalid State Machine #{field}: #{err}: was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')"
206+
end
197207
end
198208
end

lib/floe/workflow/context.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class Context
88
# @param context [Json|Hash] (default, create another with input and execution params)
99
# @param input [Hash] (default: {})
1010
def initialize(context = nil, input: nil, credentials: nil, logger: nil)
11-
context = JSON.parse(context) if context.kind_of?(String)
12-
input = JSON.parse(input || "{}")
11+
context = self.class.safe_parse("Execution Context", context) if context.kind_of?(String)
12+
input = self.class.safe_parse("Execution Input", input || "{}")
1313

1414
@context = context || {}
1515
self["Credentials"] ||= credentials || {}
@@ -21,8 +21,6 @@ def initialize(context = nil, input: nil, credentials: nil, logger: nil)
2121
self["Task"] ||= {}
2222

2323
self.logger = logger if logger
24-
rescue JSON::ParserError => err
25-
raise Floe::InvalidExecutionInput, "Invalid State Machine Execution Input: #{err}: was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')"
2624
end
2725

2826
def execution
@@ -152,6 +150,18 @@ def to_h
152150
def safe_context
153151
@context.except("Credentials")
154152
end
153+
154+
def to_json(*args)
155+
to_h.to_json(*args)
156+
end
157+
158+
private
159+
160+
def self.safe_parse(field, string)
161+
JSON.parse(string)
162+
rescue JSON::ParserError => err
163+
raise Floe::InvalidExecutionInput, "Invalid State Machine #{field}: #{err}: was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')"
164+
end
155165
end
156166
end
157167
end

0 commit comments

Comments
 (0)