From aec344577141d1db749aa62c79c0abbc4eacacb9 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Tue, 20 Oct 2020 23:57:37 +0200 Subject: [PATCH] New API. --- src/common/requests/download.cr | 4 +--- src/common/requests/login.cr | 4 +--- src/common/requests/transfer.cr | 12 +++++------- src/common/requests/upload.cr | 4 +--- src/server/main.cr | 22 +++++++++++----------- 5 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/common/requests/download.cr b/src/common/requests/download.cr index f9655ed..4662470 100644 --- a/src/common/requests/download.cr +++ b/src/common/requests/download.cr @@ -12,7 +12,7 @@ class FileStorage::Request def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) user = filestoraged.get_logged_user event - return Errors::Authorization.new @mid if user.nil? + raise NotLoggedException.new if user.nil? # FIXME: Maybe this should be moved to FileStorage::Service fd = event.fd @@ -20,8 +20,6 @@ class FileStorage::Request user_data = filestoraged.get_user_data user.uid filestoraged.storage.download self, user_data - rescue e - return Errors::GenericError.new @mid, e.to_s end end FileStorage.requests << Download diff --git a/src/common/requests/login.cr b/src/common/requests/login.cr index 40be883..424a7b4 100644 --- a/src/common/requests/login.cr +++ b/src/common/requests/login.cr @@ -25,9 +25,7 @@ class FileStorage::Request user_data = filestoraged.get_user_data user.uid - return Response::Login.new @mid - rescue e - return Errors::GenericError.new @mid, "unauthorized" + Response::Login.new @mid end end FileStorage.requests << Login diff --git a/src/common/requests/transfer.cr b/src/common/requests/transfer.cr index 435d1ab..0af6f86 100644 --- a/src/common/requests/transfer.cr +++ b/src/common/requests/transfer.cr @@ -20,18 +20,16 @@ class FileStorage::Request def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) user = filestoraged.get_logged_user event - return Errors::Authorization.new @mid if user.nil? + raise NotLoggedException.new if user.nil? # FIXME: Maybe this should be moved to FileStorage::Service fd = event.fd user_data = filestoraged.get_user_data user.uid - Baguette::Log.info "PutChunk request: #{@mid}, file #{@filedigest}, chunk: n=#{@chunk.n}, on=#{@chunk.on}, digest=#{@chunk.digest}" + Baguette::Log.debug "PutChunk request: #{@mid}, file #{@filedigest}, chunk: n=#{@chunk.n}, on=#{@chunk.on}, digest=#{@chunk.digest}" filestoraged.storage.write_chunk self, user_data - rescue e - return Errors::GenericError.new @mid, e.to_s end end FileStorage.requests << PutChunk @@ -48,16 +46,16 @@ class FileStorage::Request def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) user = filestoraged.get_logged_user event - return Errors::Authorization.new @mid if user.nil? + raise NotLoggedException.new if user.nil? # FIXME: Maybe this should be moved to FileStorage::Service fd = event.fd user_data = filestoraged.get_user_data user.uid + Baguette::Log.debug "PutChunk request: #{@mid}, file #{@filedigest}, chunk: n=#{@n}" + filestoraged.storage.read_chunk self, user_data - rescue e - return Errors::GenericError.new @mid, e.to_s end end FileStorage.requests << GetChunk diff --git a/src/common/requests/upload.cr b/src/common/requests/upload.cr index 6bf8e2e..1a7a453 100644 --- a/src/common/requests/upload.cr +++ b/src/common/requests/upload.cr @@ -10,7 +10,7 @@ class FileStorage::Request def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) user = filestoraged.get_logged_user event - return Errors::Authorization.new @mid if user.nil? + raise NotLoggedException.new if user.nil? # FIXME: Maybe this should be moved to FileStorage::Service fd = event.fd @@ -18,8 +18,6 @@ class FileStorage::Request user_data = filestoraged.get_user_data user.uid filestoraged.storage.upload self, user_data - rescue e - return Errors::GenericError.new @mid, e.to_s end end FileStorage.requests << Upload diff --git a/src/server/main.cr b/src/server/main.cr index a31422c..8d38a16 100644 --- a/src/server/main.cr +++ b/src/server/main.cr @@ -116,20 +116,20 @@ class FileStorage::Service < IPC::Server request_id = "#{request.id}" - begin - response = request.handle self, event + response = begin + request.handle self, event rescue e : AuthorizationException - Baguette::Log.error "#{request_name} authorization error" - response = FileStorage::Errors::GenericError.new request_id, "authorization error" + Baguette::Log.warning "#{request_name} authorization error" + Errors::Authorization.new request_id rescue e : AdminAuthorizationException - Baguette::Log.error "#{request_name} no admin authorization" - response = FileStorage::Errors::GenericError.new request_id, "admin authorization error" + Baguette::Log.warning "#{request_name} no admin authorization" + Errors::Authorization.new request_id rescue e : NotLoggedException - Baguette::Log.error "#{request_name} user not logged" - response = FileStorage::Errors::GenericError.new request_id, "user not logged" - # Do not handle generic exception case: do not provide a response. - # rescue e # Generic case - # Baguette::Log.error "#{request_name} generic error #{e}" + Baguette::Log.warning "#{request_name} user not logged" + Errors::GenericError.new request_id, "user not logged" + rescue e + Baguette::Log.error "#{request_name} generic error #{e}" + Errors::GenericError.new request_id, "unexpected error" end # If clients sent requests with an “id” field, it is copied