diff --git a/src/authd.cr b/src/authd.cr index 2114598..1e2b4eb 100644 --- a/src/authd.cr +++ b/src/authd.cr @@ -9,6 +9,15 @@ require "./user.cr" class AuthD::Exception < Exception end +class AuthD::MalformedRequest < Exception + getter ipc_type : Int32 + getter payload : String + + def initialize(@ipc_type, @payload) + @message = "malformed payload" + end +end + class AuthD::Response include JSON::Serializable @@ -133,9 +142,11 @@ class AuthD::Response payload = String.new message.payload type = Type.new message.utype.to_i - requests.find(&.type.==(type)).try &.from_json(payload) - rescue e : JSON::ParseException - raise Exception.new "malformed request" + begin + requests.find(&.type.==(type)).try &.from_json(payload) + rescue e : JSON::ParseException + raise MalformedRequest.new message.utype.to_i, payload + end end end @@ -307,9 +318,11 @@ class AuthD::Request payload = String.new message.payload type = Type.new message.utype.to_i - requests.find(&.type.==(type)).try &.from_json(payload) - rescue e : JSON::ParseException - raise Exception.new "malformed request" + begin + requests.find(&.type.==(type)).try &.from_json(payload) + rescue e : JSON::ParseException + raise MalformedRequest.new message.utype.to_i, payload + end end end diff --git a/src/main.cr b/src/main.cr index 7ea1027..d95697e 100644 --- a/src/main.cr +++ b/src/main.cr @@ -352,13 +352,18 @@ class AuthD::Service response = handle_request request, event.connection - info ">> #{response.class.name.sub /^Response::/, ""}" - event.connection.send response + rescue e : MalformedRequest + error "#{e.message}" + error " .. type was: #{e.ipc_type}" + error " .. payload was: #{e.payload}" + response = Response::Error.new e.message rescue e error "#{e.message}" - event.connection.send Response::Error.new e.message + response = Response::Error.new e.message end + + info ">> #{response.class.name.sub /^Response::/, ""}" end end end