Git Product home page Git Product logo

Comments (4)

yepher avatar yepher commented on August 20, 2024

@dfuentes77

One thing you could simply do in code like this is put your else if (500 <= spex.getResponseCode() && spex.getResponseCode() < 510){.. with this code so it retries once:

IRestConnection connection = new RestConnection(this.client, getEndPoint());
ResourceTransmissions.create(connection, 0, transmission);

If you want a more persistent retry you will want to be careful and have some sort of retry cap and probably an exponential backoff of some sort in case this is a recurring error. Is there a particular problem you are seeing where you are getting a log to 510 errors?

from java-sparkpost.

dfuentes77 avatar dfuentes77 commented on August 20, 2024

This is the block where that snippet is from and what we are doing. Basically if its a suppressed error, we mark it a certain way in our db. What i'm trying to do is check the response code and if it's a 50x, to not mark the db... and on the next loop of our app, it'll attempt to connect/send again (it's not very aggressive so i'm not worried about that part). But I'm never seeing it hit my responsecode check. I'm assuming the block with the< 510 means only 500-509.

catch (SparkPostErrorServerResponseException spex) {

	boolean handled = false;

	// further classify error message if possible
	try {
		Pattern p = Pattern.compile("[^\\{]*(\\{.*\\})[^\\}]*");
		Matcher m = p.matcher(spex.getMessage());

		if (m.find(0)) {
			String json = m.group(1);
			JsonParser jsonParser = new JsonParser();
			JsonObject jo = (JsonObject) jsonParser.parse(json);

			// check if the JSON response is "Suppression"
			if (jo.getAsJsonArray("errors").get(0).getAsJsonObject().get("code").getAsString().equals("1902")) {
				EmailDAO.markAsUnsubscribed(conn, em);

				Logger.log("Email not sent because user unsubscribed " + em.id);

				handled = true;

			} else if (500 <= spex.getResponseCode() && spex.getResponseCode() < 510){
				Logger.log("Sparkpost " + spex.getResponseCode() + " errors, skipping for now: " + em.id);

				handled = true;
			}
		}
	} catch (Exception ex) {
		Logger.error("Unable to determine SparkPost error: " + em.id, ex);
	}

	if (!handled) {
		Logger.error("SparkPost unknown error processing email: " + em.id, spex);
		// mark email as error first
		try {
			EmailDAO.markAsError(conn, em);
		} catch (SQLException ex) {
			Logger.error("Error marking email as error: " + em.id, ex);
		}
	}

from java-sparkpost.

yepher avatar yepher commented on August 20, 2024

I was thinking something like this. I am not quite sure how to test this since I don't know how to force a HTTP 5XX error.

private void forceFiveHundredError(int retries) throws SparkPostException, IOException {
        this.client = this.newConfiguredClient();
        IRestConnection connection = new RestConnection(this.client, "https://api.sparkpost.com/api/v1");

        try {
            // Do something to foce and internal server error
            ResourceSendingDomains.list(connection);

            throw new IllegalStateException("Error: Expected an exception");
        } catch (SparkPostException e) {
            if (e.getServerErrorResponses() != null) {
                System.out.println("Got response from server.");
                if (e.getServerErrorResponses().getResponseCode() >= 500 && e.getServerErrorResponses().getResponseCode() <= 599) {
                    System.out.println("Got a 5xx level error");
                    try {
                        // wait before retrying
                        Thread.sleep(retries * 10000);
                    } catch (InterruptedException e1) {

                    }
                    forceFiveHundredError(retries + 1);

                }
            } else {
                System.out.println("This is some other unexpected error!!!");
            }
        }
    }

from java-sparkpost.

yepher avatar yepher commented on August 20, 2024

Closing this. If the previous comment does not answer the question please re-open the ticket and let me know what I missed.

from java-sparkpost.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.