From 6f4f4b234aea9a254b1d02bf9c7fe9044dfcf689 Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Wed, 26 Feb 2020 14:54:14 +0100 Subject: [PATCH] Improved UI to print malformed requests. --- src/authd.cr | 25 +++++++++++++++++++------ src/main.cr | 11 ++++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) 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