Giter Club home page Giter Club logo

Comments (3)

beders avatar beders commented on August 19, 2024

Sounds good!


From: davidekholm [email protected]
To: beders/Resty [email protected]
Sent: Friday, September 6, 2013 8:34 AM
Subject: [Resty] Ability to read content after throwing exception (#29)

As you may know, some RESTful servers provide additional information about an error in the http content, but with today's Resty implementation one simpy gets an IOException if there is a 500 server error. Now it's impossible to dig further as the IOException carries no further information :-(
I've made a tiny and fully compatible adjustment to a subclass of Resty to handle this. I let Resty catch IOExeptions, and re-throw RestyExceptions instead. These RestyExceptions are simply subclasses of IOExceptions that embeds an AbstractResource. Problem solved :-)
I'd be happy to provide you my implementation. As I said, it's a tiny and fully compatible adjustment. It does not require any changes to existing users of your library. It just makes it better. My implementation also allows a user to add additional http request parameters which is required by some servers.
Just email me at [email protected] and I'll contribute my code!

Reply to this email directly or view it on GitHub.

from resty.

davidekholm avatar davidekholm commented on August 19, 2024

Great to hear from you.
Here's the updated code. Note I subclassed your existing classes. I'd naturally recommend you to implement these changes in the corresponding base classes instead. As you see, I'm simply catching IOException, and if it's possible to read a content reply, I do so and wrap the result as a member variable of RestyException (which in turn is an IOException)

These changes won't affect any clients, just give the ability to inspect error details further.

However, the attached code doesn't include my other addition, which enables the user to add and remove custom http headers. I recommend that you add that ability to the base class too.

Here are the code snippets that I added to implement support for custom http headers. My additions in bold (Resty.html):

private Map<String, String> additionalHeaders; // member variable of Resty

protected <T extends AbstractResource> URLConnection openConnection(URI anUri, T resource) throws IOException, MalformedURLException {
    URLConnection con = anUri.toURL().openConnection(proxy);
    addStandardHeaders(con, resource);
    addAdditionalHeaders(con); // Added line 
    for (Option o : options) {
        o.apply(con);
    }
    return con;
}

/** Add all headers that have been set with the alwaysSend call. */
protected void addAdditionalHeaders(URLConnection con) {
    for (Map.Entry<String, String> header : getAdditionalHeaders().entrySet()) {
        con.addRequestProperty(header.getKey(), header.getValue());
    }
}

/**
 * Tell Resty to send the specified header with each request done on this instance. These headers will also be sent from any resource object returned by this instance. I.e. chained calls will carry
 * over the headers r.json(url).json(get("some.path.to.a.url")); Multiple headers of the same type are not supported (yet).
 * 
 * @param aHeader
 *          the header to send
 * @param aValue
 *          the value
 */
public void alwaysSend(String aHeader, String aValue) {
    getAdditionalHeaders().put(aHeader, aValue);
}

/**
 * Don't send a header that was previously added in the alwaysSend method.
 * 
 * @param aHeader
 *          the header to remove
 */
public void dontSend(String aHeader) {
    getAdditionalHeaders().remove(aHeader);
}

protected Map<String, String> getAdditionalHeaders() {
    if (additionalHeaders == null) {
        additionalHeaders = new HashMap<String, String>();
    }
    return additionalHeaders;
}

Regards
/David

On 7 sep 2013, at 20:14, Jochen Bedersdorfer [email protected] wrote:

Sounds good!


From: davidekholm [email protected]
To: beders/Resty [email protected]
Sent: Friday, September 6, 2013 8:34 AM
Subject: [Resty] Ability to read content after throwing exception (#29)

As you may know, some RESTful servers provide additional information about an error in the http content, but with today's Resty implementation one simpy gets an IOException if there is a 500 server error. Now it's impossible to dig further as the IOException carries no further information :-(
I've made a tiny and fully compatible adjustment to a subclass of Resty to handle this. I let Resty catch IOExeptions, and re-throw RestyExceptions instead. These RestyExceptions are simply subclasses of IOExceptions that embeds an AbstractResource. Problem solved :-)
I'd be happy to provide you my implementation. As I said, it's a tiny and fully compatible adjustment. It does not require any changes to existing users of your library. It just makes it better. My implementation also allows a user to add additional http request parameters which is required by some servers.
Just email me at [email protected] and I'll contribute my code!

Reply to this email directly or view it on GitHub.

Reply to this email directly or view it on GitHub.

from resty.

jpasqua avatar jpasqua commented on August 19, 2024

Any chance that the exception handling changes will be folded into Resty? It would be really helpful.

from resty.

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.