Henry Yang
Henry Yang

Reputation: 2613

HTTP status code when sending email failed

Note: I have read this but I still don't know how to go about building the sending email function correctly, so I ask this question. I need to know the HTTP status code to use when email sending succeed/failed, or if that's not the right thing to do, the right thing to do.

A POST request to my rails app will send an email.

If the email sending failed, what HTTP status code should I return to the person who send the POST request in my JSON response?

  def inform
    delivered = true
    begin
      UserMailer.new_comment(current_user, other_user, @note).deliver_now
    rescue Net::SMTPAuthenticationError, Net::SMTPServerBusy, Net::SMTPSyntaxError, Net::SMTPFatalError, Net::SMTPUnknownError
      delivered = false
    end

    if delivered
      # I use :created here because email is created
      render json: { delivered: true }.to_json, status: :created
    else
      # I use :service_unavailable here because email sending failed
      render json: { delivered: false }.to_json, status: :service_unavailable
    end
  end

Upvotes: 8

Views: 9323

Answers (2)

B Seven
B Seven

Reputation: 45941

502 - bad_gateway

Typically used for upstream server failure.

Here's some more info: https://airbrake.io/blog/http-errors/502-bad-gateway-error

a 502 Bad Gateway Error means that a server that is upstream to one that you (the client) are connecting to has run into trouble. In this scenario, this means that the server providing the 502 Bad Gateway Error is acting as a gateway

Upvotes: 12

Nikita Leshchev
Nikita Leshchev

Reputation: 1844

I would rather use code 424 https://www.rfc-editor.org/rfc/rfc4918#section-11.4

The 424 (Failed Dependency) status code means that the method could not be performed on the resource because the requested action depended on another action and that action failed. For example, if a command in a PROPPATCH method fails, then, at minimum, the rest of the commands will also fail with 424 (Failed Dependency).

Upvotes: 1

Related Questions