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

SendMessage is not throwing http errors #28

Open
mpiliszcz opened this issue Oct 18, 2017 · 0 comments
Open

SendMessage is not throwing http errors #28

mpiliszcz opened this issue Oct 18, 2017 · 0 comments
Labels

Comments

@mpiliszcz
Copy link

When network issues occur, e.g. no network connection, the low level error code is not returned.
This could be the solution:

    private SentMessageData SendMessage(RestRequest request)
    {
        request.RequestFormat = DataFormat.Json;
        request.JsonSerializer = NewtonsoftJsonSerializer.Default;

        var result = WebClient.Execute(request);

        if (result.ResponseStatus == ResponseStatus.Completed && (result.StatusCode == HttpStatusCode.NoContent))
            return null;

        if (result.ResponseStatus == ResponseStatus.Completed && result.StatusCode == HttpStatusCode.Unauthorized)
            throw new UnauthorizedAccessException("MailJet returned an HTTP 401 exception, please check your credentials");

        if (result.ResponseStatus == ResponseStatus.Completed && result.StatusCode >= HttpStatusCode.BadRequest)
            throw new HttpException((int)result.StatusCode, result.StatusCode.ToString());

        if (result.ErrorMessage != null && String.IsNullOrWhiteSpace(result.Content))
        {
            throw new Exception(result.ErrorMessage);
        }
        else
        {
            var error = JsonConvert.DeserializeObject<ErrorResponse>(result.Content);
            if (!String.IsNullOrWhiteSpace(error.ErrorInfo) || !String.IsNullOrWhiteSpace(error.ErrorMessage))
                throw new Exception(String.Format("{0}\n{1}", error.ErrorMessage, error.ErrorMessage));
        }

        var data = JsonConvert.DeserializeObject<SentMessageData>(result.Content);
        return data;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants