Re: [Simple-support] Handling deserialization exceptions
Brought to you by:
niallg
|
From: Mike G. <mik...@ca...> - 2012-06-18 15:09:46
|
Hi, On the subject of exception handling, - Is it possible to retrieve line numbers for the cause of the exception? - Is it possible to store line numbers on elements to relate problems with valid xml that deserialises but has application specific issues which are discovered after deserialisation? - Is it possible to add exception codes to the simple exceptions so that different issues can be handled in a user friendly way? Even better provide details that can be extracted from the exception. e.g. code 123==MISSING ATTRIBUTE, then perhaps after casting the exception extra fields could contain relevant information, e.g. attribute name Simple is great, but from my point of view, I would like to make an application that accepts user created xml and is able to report issues on its own terms without showing the simple error messages/stack traces directly. (in my case it is a build tool so user friendliness is not super critical, but this is still a deficiency) regards, Mike On Tue, Jun 12, 2012 at 10:48 AM, Niall Gallagher <gal...@ya... > wrote: > check out Persister.read(MyClass.class, source, false), http://simple.sourceforge.net/download/stream/doc/javadoc/org/simpleframework/xml/core/Persister.html#read(java.lang.Class, > java.io.InputStream, boolean) it allows you to read only what your > interested in > > > --- On *Mon, 11/6/12, Митя Балахонский <dn...@gm...>* wrote: > > > From: Митя Балахонский <dn...@gm...> > Subject: Re: [Simple-support] Handling deserialization exceptions > To: "Niall Gallagher" <gal...@ya...> > Cc: sim...@li... > Received: Monday, 11 June, 2012, 9:59 PM > > > Thanks Niall! > > In this simple example - yes, that would be a pretty straightforward way > to achieve it. > But in real life it is much more complex, not just 1 object with 2 fields, > but tens and hundreds, and the server sometimes sends me an epic bullshit, > I simply can't predict, where the exception can occur. > And that's the case the exceptions were made for! You simply put "try" > around the suspicious code, no matter how complex it is, and in the end you > just have "Ok" or "Exception" that you can handle. > If I do all the validation by hand, this would lead me to a clumsy > unreadable error-prone code, that I wanted to avoid and that's why I've > chosen your framework. > > It would be great if Simple Framework supported something like this: > > @Root(name="order") > class Order { > @Attribute(name="id", required=true) > int id; > @Element(name="name", required=false) > String name; > > @HandleException > public boolean handleException(Exception e) { > Log.w(TAG, "Cannot parse Order, " + e); > return false; // false because we don't want to insert it into the > list > // Or we can throw it again > } > } > > // Or, the non-intrusive way > @ElementList(stopOnValidationErrors = false) > List<Order> orders; > > Wouldn't that be too insolent of me to suggest a new feature? :) > > I'll try to add support for it and then show you the result. > But if you think that's unreasonable, please let me know and prevent me > from digging into the code :) > > Thanks in advance! > > 2012/6/12 Niall Gallagher <gal...@ya...<http://mc/compose?to=...@ya...> > > > > the order attribute must have required=false, take a look at the @Validate > annotation, you can simply remove any orders that have a null id. > > --- On *Mon, 11/6/12, Митя Балахонский <dn...@gm...<http://mc/compose?to=...@gm...> > >* wrote: > > > From: Митя Балахонский <dn...@gm...<http://mc/compose?to=...@gm...> > > > Subject: [Simple-support] Handling deserialization exceptions > To: sim...@li...<http://mc/compose?to=...@li...> > Received: Monday, 11 June, 2012, 11:28 AM > > Hello, dear All > > I'm very happy that such a thing as Simple Framework exists, it helps > me a lot :) > But now I have an issue: > I'm using XML for client-server data exchange, and server doesn't > always send me valid data. > For example: > > <response> > <order id="123"> > <name>Foo</name> > </order> > <order> > <name>Bar</name> > </order> > <order id="456"> > <name>Alice</name> > </order> > </response> > > I have a simple class for it: > > @Root(name="order") > class Order { > @Attribute(name="id", required=true) > int id; > @Element(name="name", required=false) > String name; > } > > @Root(name="response") > class Response { > @ElementList > List<Order> orders; > } > > And an @ElementList of Order in the Response class. > > It works perfectly until server sends me some order without the "id" > attribute. In this case parsing fails and I have no way to receive > other orders that were correctly formed, because framework exception > fires and cancels all the parsing. > Is there a proper way to handle it with Simple Framework? > I'd like to have a list of orders, which were parsed correctly, and > ignore incorrect ones. With my example that would be a list, containing > Foo and > Alice, maybe having some notifications that 1 order cannot be parsed > because of the exception. > > Thanks in advance! > > -- > Best regards, > Dmitry Balakhonsky > > > ------------------------------------------------------------------------------ > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > Simple-support mailing list > Sim...@li...<http://au.mc1610.mail.yahoo.com/mc/compose?to=...@li...> > https://lists.sourceforge.net/lists/listinfo/simple-support > > > > > -- > Best regards, > Dmitry Balakhonsky > > > > ------------------------------------------------------------------------------ > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > Simple-support mailing list > Sim...@li... > https://lists.sourceforge.net/lists/listinfo/simple-support > > |