Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 914
Henry Yang Avatar asked Jan 09 '19 23:01

Henry Yang


People also ask

What is 204 status code HTTP?

The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page.

What is HTTP status code1?

We tend to get -1 status codes when there are network issues or connection problems, so we display the user a network problems page in those cases.

What is the difference between 200 and 201 status code?

A 200-level response means that everything is working exactly as it should. 200: “Everything is OK.” This is the code that is delivered when a web page or resource acts exactly the way it's expected to. 201: “Created.” The server has fulfilled the browser's request, and as a result, has created a new resource.

What does a HTTP 200 response status mean?

The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.


Video Answer


2 Answers

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

like image 106
B Seven Avatar answered Oct 16 '22 17:10

B Seven


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).

like image 1
Nikita Leshchev Avatar answered Oct 16 '22 18:10

Nikita Leshchev