@@ -9,7 +9,9 @@ class TsaLetterController < ApplicationController
99 before_action { authorize :tsa_letter , :access? }
1010
1111 ERROR_MAP = {
12+ 400 => Common ::Exceptions ::BadRequest ,
1213 401 => Common ::Exceptions ::Unauthorized ,
14+ 403 => Common ::Exceptions ::Forbidden ,
1315 500 => Common ::Exceptions ::ExternalServerInternalServerError ,
1416 501 => Common ::Exceptions ::NotImplemented
1517 } . freeze
@@ -22,11 +24,14 @@ def show
2224 serialized = most_recent_letter ( files_metadata )
2325 render ( json : serialized )
2426 rescue Common ::Client ::Errors ::ClientError => e
25- handle_error ( e )
27+ handle_show_error ( e )
2628 end
2729
2830 def download
29- files_metadata = fetch_letter_metadata
31+ files_metadata = handle_download_error ( 'TSA Letter Error on download validation' ) do
32+ fetch_letter_metadata
33+ end
34+
3035 user_owns_file = files_metadata . any? do |file |
3136 params [ :id ] == file [ 'uuid' ] && params [ :version_id ] == file [ 'currentVersionUuid' ]
3237 end
@@ -35,7 +40,9 @@ def download
3540 end
3641
3742 download_service = ClaimsEvidenceApi ::Service ::Files . new
38- letter_response = download_service . download ( params [ :id ] , params [ :version_id ] )
43+ letter_response = handle_download_error ( 'TSA Letter Download Error' ) do
44+ download_service . download ( params [ :id ] , params [ :version_id ] )
45+ end
3946 send_data (
4047 letter_response . body ,
4148 type : 'application/pdf' ,
@@ -74,20 +81,36 @@ def most_recent_letter(files)
7481 source : self . class . name
7582 end
7683
77- def handle_error ( error )
84+ def handle_show_error ( error )
7885 status = error . respond_to? ( :status ) && error . status
7986 case status
80- when *ERROR_MAP . keys
81- error_class = ERROR_MAP [ status ]
82- raise error_class
8387 when *NONBLOCKING_STATUSES
84- Rails . logger . info ( 'TSA Letter Error' ,
88+ Rails . logger . info ( 'Non-blocking TSA Letter Error' ,
8589 error_status : status ,
8690 user_account_id : current_user . user_account_uuid )
8791 render ( json : { data : nil } )
92+ when *ERROR_MAP . keys
93+ error_class = ERROR_MAP [ status ]
94+ raise error_class
8895 else
8996 raise error
9097 end
9198 end
99+
100+ def handle_download_error ( description , &command )
101+ yield ( command )
102+ rescue Common ::Client ::Errors ::ClientError => e
103+ status = e . respond_to? ( :status ) && e . status
104+ case status
105+ when *ERROR_MAP . keys
106+ Rails . logger . info ( description ,
107+ error_status : status ,
108+ user_account_id : current_user . user_account_uuid )
109+ error_class = ERROR_MAP [ status ]
110+ raise error_class
111+ else
112+ raise e
113+ end
114+ end
92115 end
93116end
0 commit comments