-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate user info block configuration #52
Conversation
Can you boot an incorrectly configured application and show a screenshot of the expected errors in the "Testing" section of the PR description? |
BUFFER_LENGTH_ERROR = "buffer_length must be an Integer" | ||
DEVELOPMENT_ERROR = "development option must be a boolean" | ||
|
||
MISSING_BLOCK_ERROR = <<~MESSAGE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use of the squiggly heredoc!
packages/ruby/lib/readme/errors.rb
Outdated
REJECT_PARAMS_ERROR = "reject_params option must be an array of strings" | ||
ALLOW_ONLY_ERROR = "allow_only option must be an array of strings" | ||
BUFFER_LENGTH_ERROR = "buffer_length must be an Integer" | ||
DEVELOPMENT_ERROR = "development option must be a boolean" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on capitalizing the first letter of these errors? Or are they being inserted into an existing sentence?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on wrapping the options in backticks? Like:
`reject_params` option must be an array of strings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestions. I made a couple of changes.
packages/ruby/lib/readme/errors.rb
Outdated
class BadBlockMessage | ||
def self.call(result) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on making this a class method Readme::Errors.bad_block_message
rather than a callable constant? I think that might be a bit more idiomatic.
packages/ruby/lib/readme/metrics.rb
Outdated
begin | ||
user_info = @get_user_info.call(env) | ||
raise if user_info_invalid?(user_info) | ||
rescue | ||
puts Errors::BadBlockMessage.call(user_info) | ||
return [status, headers, body] | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we need a begin/end
block here if we're manually raising? This feels like using exceptions for control-flow. Thoughts on expressing this as a conditional instead?
user_info = @get_user_info.call(env)
if user_info_invalid?(user_info)
puts Errors::BadBlockMessage.call(user_info)
else
payload = Payload.new(har, user_info, development: @development)
@@request_queue.push(payload.to_json)
end
[status, headers, body]
I could see extracting the conditional into a private method so call
doesn't get too cluttered
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Part of what I wanted to do here was catch the situation where the block itself raises an exception so the application can continue running. Does that make sense to do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you're saying. I kind of feels like feature creep though? We have a separate card for not crashing the client request if the middleware has an issue. I think we'll want a single rescue around the whole process.
If the block raises, then we can't send a valid payload to Readme so I don't think there is much value to adding a local rescue just for the block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great that makes sense. I'll update this to not use rescue block.
packages/ruby/lib/readme/errors.rb
Outdated
module Errors | ||
API_KEY_ERROR = "Missing API Key" | ||
REJECT_PARAMS_ERROR = "reject_params option must be an array of strings" | ||
ALLOW_ONLY_ERROR = "allow_only option must be an array of strings" | ||
BUFFER_LENGTH_ERROR = "buffer_length must be an Integer" | ||
DEVELOPMENT_ERROR = "development option must be a boolean" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for making these constants!
f5fda7a
to
4f52fd2
Compare
This commit adds some validation around user info block required by the middleware. One check runs at application boot and ensures that the block is not nil. The other checks occur at run time and ensure that if the block is not configured properly, the application will not crash and log an error instead. The request to the Readme API will be aborted and the response will be passed on through the rest of the middleware stack.
4f52fd2
to
52bafff
Compare
🧰 What's being changed?
This commit adds some validation around user info block required by the
middleware.
One check runs at application boot and ensures that the block is not
nil.
The other checks occur at run time and ensure that if the block is not
configured properly, the application will not crash and log an error
instead. The request to the Readme API will be aborted and the response
will be passed on through the rest of the middleware stack.
🧪 Testing
The new checks are covered by the automated test suite.
Without a block configured:
With an improperly configured block (missing label value)
Running a request produces the following log and the request to the client returns a 200:
