Reputation: 361
I have the code below and even when it goes into the rescue
block it ignores the status 500
and always returns a 200 response code... what am I doing wrong here? Thanks!
post /deletebranch do
# setup my variables here
if checkout_sha.eql? '0000000000000000000000000000000000000000'
delete_branch(endpoint, repo_path, config_obj)
end
end
def delete_branch(endpoint, repo_path, config_obj)
base = config_obj['base_dir']
if Dir.exists?(base) and Dir.exists?("#{base}/#{repo_path}")
stream do |body_content|
body_content << "endpoint: #{endpoint}\n"
body_content << "repo_path: #{repo_path}\n"
body_content << "base: #{base}\n"
body_content << "\n"
body_content << "Attempting to remove '#{repo_path}' from inside '#{base}'\n"
logger.info("Attempting to remove '#{repo_path}' from inside '#{base}'")
begin
Dir.chdir(base)
FileUtils.remove_entry_secure(repo_path, force = false)
rescue => e
logger.info('This exception was thrown:')
logger.error(e)
body_content << 'This exception was thrown:'
body_content << e
body_content << "\n"
status 500
end
end
end
end
Upvotes: 0
Views: 757
Reputation: 59611
You cannot change the response code (200
, 500
etc.) after you've begun to return the response body. Because you are using stream
it means you've been sending the response body to the browser far before your code hits status 500
, and the response code must come before the body.
It seems that you are sending a very small amount of data, so you don't need to stream at all. Try doing something as follows:
post '/deletebranch' do
# setup my variables here
if checkout_sha.eql? '0000000000000000000000000000000000000000'
delete_branch(endpoint, repo_path, config_obj)
end
end
def delete_branch(endpoint, repo_path, config_obj)
base = config_obj['base_dir']
status_code = 200
body_content = ""
if Dir.exists?(base) and Dir.exists?("#{base}/#{repo_path}")
body_content << "endpoint: #{endpoint}\n"
body_content << "repo_path: #{repo_path}\n"
body_content << "base: #{base}\n"
body_content << "\n"
body_content << "Attempting to remove '#{repo_path}' from inside '#{base}'\n"
logger.info("Attempting to remove '#{repo_path}' from inside '#{base}'")
begin
Dir.chdir(base)
FileUtils.remove_entry_secure(repo_path, force = false)
rescue => e
logger.info('This exception was thrown:')
logger.error(e)
body_content << 'This exception was thrown:'
body_content << e
body_content << "\n"
status_code = 500
end
end
[status_code, body_content]
end
Upvotes: 1