Skip to content

Commit

Permalink
Add better error handling around cost center csv file getting.
Browse files Browse the repository at this point in the history
* Explicit error message if env file is not set
* doesn't load fake data unless running in development
*errors out if unable to get csv file from s3
  • Loading branch information
CorinWilkins committed Oct 1, 2024
1 parent 495df3f commit ac7b352
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions config/initializers/cost_centers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ def load_from_s3(bucket, key)
end

def get_cost_centre_data()
if Rails.env.development?
return File.read(File.join(Rails.root, 'config', 'cost_centre_fixture.csv'))
end

begin
return load_from_s3(ENV['COST_CENTRE_S3_BUCKET_NAME'], 'cost_centres.csv')
bucket = ENV['COST_CENTRE_S3_BUCKET_NAME']
raise 'Failed COST_CENTRE_S3_BUCKET_NAME env var is NOT set' unless bucket

return load_from_s3(bucket, 'cost_centres.csv')
rescue Aws::S3::Errors::ServiceError, Aws::Errors::MissingRegionError
Rails.logger.error("Failed unable to retrieve cost_centres.csv from s3")
return File.read(File.join(Rails.root, 'config', 'cost_centre_fixture.csv'))
raise "Failed unable to retrieve cost_centres.csv from s3"
end
end

Expand Down

0 comments on commit ac7b352

Please sign in to comment.