Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Provide users possible status codes that SendGrid can return #1155

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SendGrid/Helpers/Errors/ErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static async Task ThrowException(HttpResponseMessage message)
/// <returns>Return string with the error Status Code and the Message</returns>
private static async Task<string> GetErrorMessage(HttpResponseMessage message)
{
var errorStatusCode = (int)message.StatusCode;
var errorStatusCode = (SendGridStatusCode)message.StatusCode;
var errorReasonPhrase = message.ReasonPhrase;

string errorValue = null;
Expand Down
5 changes: 3 additions & 2 deletions src/SendGrid/Helpers/Errors/Model/SendGridErrorResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
public class SendGridErrorResponse
{
/// <summary>
/// Gets or sets the error Status Code
/// Gets or sets the error Status Code.<br/>
/// Might be any <see cref="System.Net.HttpStatusCode"/>, not just ones in <see cref="SendGridStatusCode"/>.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens to this property when the value is not defined in SendGridStatusCode? Guessing null and worried that clients would then not get the actual status code. Could this be added as an extra property of SendGridErrorResponse so it's backwards-compatible and supports the benefits of the enum?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enums are value type, so cannot be null. If value of ErrorHttpStatusCode is not defined in SendGridStatusCode, it will still be stored in ErrorHttpStatusCode (Visual Studio will just show you int value instead of a name).

Admittedly, this might be not the best solution since value of ErrorHttpStatusCode might be outside of what's defined SendGridStatusCode, but no information will be lost.

/// </summary>
public int ErrorHttpStatusCode { get; set; }
public SendGridStatusCode ErrorHttpStatusCode { get; set; }

/// <summary>
/// Gets or sets the error Reason Phrase
Expand Down
105 changes: 105 additions & 0 deletions src/SendGrid/Helpers/Errors/Model/SendGridStatusCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
namespace SendGrid.Helpers.Errors.Model
{
/// <summary>
/// Each SMTP call you make returns a response.
/// 200 responses are usually success responses, and 400 responses are usually deferrals.
/// SendGrid continues to retry resending 400 messages for up to 72 hours.
/// 500 responses are hard failures that are not retried by our servers.
/// </summary>
/// <remarks>
/// <see href="https://docs.sendgrid.com/for-developers/sending-email/smtp-errors-and-troubleshooting"/>.
/// </remarks>
public enum SendGridStatusCode
{
/// <summary>
/// Your mail has been successfully queued!
/// This response indicates that the recipient server has accepted the message.
/// </summary>
QueuedForDelivery = 250,

/// <summary>
/// This means the "from" address does not match a verified Sender Identity.
/// Mail cannot be sent until this error is resolved.
/// </summary>
/// <remarks>
/// To learn how to resolve this error, see our <see href="https://docs.sendgrid.com/for-developers/sending-email/sender-identity">Sender Identity requirements</see>.
/// </remarks>
InvalidFromAddress = 403,

/// <summary>
/// Messages are temporarily deferred because of recipient server policy - often it's because of too many messages or connections in too short of a timeframe.
/// </summary>
/// <remarks>
/// We continue to retry deferred messages for up to 72 hours.
/// Consider temporarily sending less messages to a domain that is returning this code because this could further delay your messages currently being tried.
/// </remarks>
MessagesDeferred = 421,

/// <summary>
/// The message failed because the recipient's mailbox was unavailable, perhaps because it was locked or was not routable at the time.
/// </summary>
/// <remarks>
/// We continue to retry messages for up to 72 hours.
/// Consider temporarily sending less messages to a domain that is returning this code because this could further delay your messages currently being tried.
/// </remarks>
MailboxUnavailable = 450,

/// <summary>
/// There is a credit limit of emails per day enforced in error.
/// </summary>
/// <remarks>
/// <see href="https://support.sendgrid.com/hc">Contact support</see> to remove that limit.
/// </remarks>
MaximumCreditsExceeded = 451,

/// <summary>
/// The message has been deferred due to insufficient system storage.
/// </summary>
/// <remarks>
/// We continue to retry messages for up to 72 hours.
/// </remarks>
TooManyRecipientsThisHour = 452,

/// <summary>
/// The user’s mailbox was unavailable.
/// Usually because it could not be found, or because of incoming policy reasons.
/// </summary>
/// <remarks>
/// Remove these address from your list - it is likely a fake, or it was mistyped.
/// </remarks>
InvalidMailbox = 550,

/// <summary>
/// The intended mailbox does not exist on this recipient server.
/// </summary>
/// <remarks>
/// Remove these addresses from your list.
/// </remarks>
UserDoesNotExist = 551,

/// <summary>
/// The recipients mailbox has exceeded its storage limits.
/// </summary>
/// <remarks>
/// We don't resend messages with this error code because this is usually a sign this is an abandoned email.
/// </remarks>
MailboxIsFull = 552,

/// <summary>
/// The message was refused because the mailbox name is either malformed or does not exist.
/// </summary>
/// <remarks>
/// Remove these addresses from your list.
/// </remarks>
InvalidUser = 553,

/// <summary>
/// This is a default response that can be caused by a lot of issues.
/// </summary>
/// <remarks>
/// There is often a human readable portion of this error that gives more detailed information, but if not,
/// remove these addresses from your list.
/// </remarks>
MailRefused = 554,
}
}
2 changes: 1 addition & 1 deletion tests/SendGrid.Tests/Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6059,7 +6059,7 @@ public async void TestHttpErrorAsException()

var errorResponseExpected = new SendGridErrorResponse
{
ErrorHttpStatusCode = 503,
ErrorHttpStatusCode = (SendGridStatusCode)503,
ErrorReasonPhrase = "Service Unavailable",
SendGridErrorMessage = "error message",
FieldWithError = "field value",
Expand Down