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
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
begin
requests.find(&.type.==(type)).try &.from_json(payload)
rescue e : JSON::ParseException
raise Exception.new "malformed request"
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
begin
requests.find(&.type.==(type)).try &.from_json(payload)
rescue e : JSON::ParseException
raise Exception.new "malformed request"
raise MalformedRequest.new message.utype.to_i, payload
end
end
end

View File

@ -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