Skip to content

Commit

Permalink
Merge pull request #12 from taylorfinnell/dont-spam-error
Browse files Browse the repository at this point in the history
Allow configuration to not raise on certain common errors
  • Loading branch information
taylorfinnell committed Oct 4, 2019
2 parents d811082 + 61ad0ea commit 779fef0
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 24 deletions.
18 changes: 0 additions & 18 deletions .github/workflows/crystal.yml

This file was deleted.

23 changes: 23 additions & 0 deletions spec/hqtrivia/config_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "../spec_helper"

module HqTrivia
describe Config do
it "can configure" do
HqTrivia.config.supress_missing_type_attribute_json_errors.should eq(false)

HqTrivia.configure do |config|
config.supress_missing_type_attribute_json_errors = true
end

HqTrivia.config.supress_missing_type_attribute_json_errors.should eq(true)
ensure
HqTrivia.configure do |config|
config.supress_missing_type_attribute_json_errors = false
end
end

it "defaults to supress_missing_type_attribute_json_errors being false" do
HqTrivia.config.supress_missing_type_attribute_json_errors.should eq(false)
end
end
end
36 changes: 36 additions & 0 deletions spec/hqtrivia/model/raw_web_socket_message_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "../../spec_helper"

module HqTrivia::Model
describe RawWebSocketMessage do
it "raises if if type is missing in thhe json and supress_missing_type_attribute_json_errors is false" do
json = {"blah" => 1}.to_json

expect_raises(RawWebSocketMessage::JSONParseError, /Could not parse '{"blah":1}'/) do
RawWebSocketMessage.decode(json)
end
end

it "gives an unknown message if type is missing from json and supress_missing_type_attribute_json_errors is true" do
json = {"blah" => 1}.to_json

HqTrivia.configure do |config|
config.supress_missing_type_attribute_json_errors = true
end

msg = RawWebSocketMessage.decode(json)

msg.is_a?(Model::UnknownMessage).should eq(true)
msg.as(Model::UnknownMessage).json.should eq("{\"blah\":1}")
ensure
HqTrivia.configure do |config|
config.supress_missing_type_attribute_json_errors = false
end
end

it "errors on invalid json" do
expect_raises(RawWebSocketMessage::JSONParseError, /Could not parse '{a:}'/) do
msg = RawWebSocketMessage.decode("{a:}")
end
end
end
end
10 changes: 10 additions & 0 deletions src/hqtrivia.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module HqTrivia
# :nodoc:
@@AUTH = Auth.new

@@CONFIG = Config.new

# HqTrivia logging instance
def self.logger
@@LOGGER
Expand All @@ -23,6 +25,14 @@ module HqTrivia
@@AUTH
end

def self.config
@@CONFIG
end

def self.configure
yield config
end

# When an active `Show` is seen, it's yielded to the block
def self.on_show(coordinator : Coordinator, blocking = true, &block : Model::Show ->)
show_coordinator = ShowCoordinator.new(coordinator)
Expand Down
8 changes: 8 additions & 0 deletions src/hqtrivia/config.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module HqTrivia
class Config
property supress_missing_type_attribute_json_errors

def initialize(@supress_missing_type_attribute_json_errors = false)
end
end
end
22 changes: 16 additions & 6 deletions src/hqtrivia/model/raw_web_socket_message.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ module HqTrivia
module Model
# A raw JSON message coming off the socket.
class RawWebSocketMessage
class JSONParseError < Exception
end

JSON.mapping({
type: String,
})
Expand All @@ -12,16 +15,23 @@ module HqTrivia
begin
decoded = {{@type}}.from_json({{json}})
case decoded.type
{% for msg, index in Model::MessageTypes.constant("MESSAGE_LIST") %}
when {{msg}}
Model::{{msg.camelcase.id}}.from_json({{json}})
{% end %}
{% for msg, index in Model::MessageTypes.constant("MESSAGE_LIST") %}
when {{msg}}
Model::{{msg.camelcase.id}}.from_json({{json}})
{% end %}
else
Model::UnknownMessage.new({{json}}, Time.utc)
end
rescue jme : JSON::MappingError
HqTrivia.logger.error("Failed to parse json: #{{{json}}}")
raise jme
if jme.message =~ /Missing JSON attribute: type/
if !HqTrivia.config.supress_missing_type_attribute_json_errors
raise HqTrivia::Model::RawWebSocketMessage::JSONParseError.new("Could not parse '#{{{json}}}'")
end
end

Model::UnknownMessage.new({{json}}, Time.utc)
rescue jpe : JSON::ParseException
raise HqTrivia::Model::RawWebSocketMessage::JSONParseError.new("Could not parse '#{{{json}}}'")
end
end
end
Expand Down

0 comments on commit 779fef0

Please sign in to comment.