Improved UI to print malformed requests.

This commit is contained in:
Luka Vandervelden 2020-02-26 14:54:14 +01:00
parent 2678ccaf9f
commit 6f4f4b234a
2 changed files with 27 additions and 9 deletions

View File

@ -9,6 +9,15 @@ require "./user.cr"
class AuthD::Exception < Exception class AuthD::Exception < Exception
end 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 class AuthD::Response
include JSON::Serializable include JSON::Serializable
@ -133,9 +142,11 @@ class AuthD::Response
payload = String.new message.payload payload = String.new message.payload
type = Type.new message.utype.to_i type = Type.new message.utype.to_i
requests.find(&.type.==(type)).try &.from_json(payload) begin
rescue e : JSON::ParseException requests.find(&.type.==(type)).try &.from_json(payload)
raise Exception.new "malformed request" rescue e : JSON::ParseException
raise MalformedRequest.new message.utype.to_i, payload
end
end end
end end
@ -307,9 +318,11 @@ class AuthD::Request
payload = String.new message.payload payload = String.new message.payload
type = Type.new message.utype.to_i type = Type.new message.utype.to_i
requests.find(&.type.==(type)).try &.from_json(payload) begin
rescue e : JSON::ParseException requests.find(&.type.==(type)).try &.from_json(payload)
raise Exception.new "malformed request" rescue e : JSON::ParseException
raise MalformedRequest.new message.utype.to_i, payload
end
end end
end end

View File

@ -352,13 +352,18 @@ class AuthD::Service
response = handle_request request, event.connection response = handle_request request, event.connection
info ">> #{response.class.name.sub /^Response::/, ""}"
event.connection.send 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 rescue e
error "#{e.message}" error "#{e.message}"
event.connection.send Response::Error.new e.message response = Response::Error.new e.message
end end
info ">> #{response.class.name.sub /^Response::/, ""}"
end end
end end
end end