You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Mandy Warren <ma...@gmail.com> on 2013/09/19 22:56:21 UTC
How to handle exceptions from a SOAP webservice in a single point
Hi,
I would like to handle all exceptions thrown by my webservice in a single
point so I can create Faults with specific codes / strings based on info in
the exception. When using JAX-RS I can do this very neatly via an Exception
Mapper (and then convert to a REST response) but there doesn't seem to be
an equivalent for JAX-WS..
Is there a way to handle the exceptions before they are turned into SOAP
Faults?
Many thanks
Mandy
RE: How to handle exceptions from a SOAP webservice in a single
point
Posted by Andrei Shakirin <as...@talend.com>.
Hi,
You can just set it back to the exchange:
message.getExchange().set(Exception.class, soapFaultException);
Your interceptor should be registered somewhere on beginning of the outbound chain, before exception will be marshalled to SOAP fault.
Regards,
Andrei.
> -----Original Message-----
> From: Mandy Warren [mailto:mandys.inbox@gmail.com]
> Sent: Freitag, 20. September 2013 16:37
> To: users@cxf.apache.org
> Subject: Re: How to handle exceptions from a SOAP webservice in a single
> point
>
> Thanks Andrei,
>
> just one thing, now I have the new fault how do I return it? The
> handleMessage(SoapMessage message) method returns a void so do I just
> throw the new SOAPFaultException or if not, how do I set the new fault in
> the message param?
>
> Sent from a mobile device
>
> On 20 Sep 2013, at 13:46, Andrei Shakirin <as...@talend.com> wrote:
>
> > Hi,
> >
> > Regarding the first question you can do something like:
> >
> > fault = (SOAPFactory.newInstance()).createFault();
> > fault.setFaultString(BUSINESS_FAULT_STRING);
> > String errorString = businessException.getMessage();
> > try {
> > // 2. Parse exception string and add it as SOAPFault Details
> > Document faultDoc = builder.parse(new ByteArrayInputStream(
> > errorString.getBytes()));
> > Detail detail = fault.addDetail();
> > Document owner = detail.getOwnerDocument();
> > Node details =
> owner.importNode(faultDoc.getDocumentElement(), true);
> > detail.appendChild(details);
> > } catch (SAXException e) {
> > // 3. If exception string is not XML, set is as fault string
> > fault.setFaultString(errorString);
> > }
> > return new SOAPFaultException(fault);
> >
> >
> > Regards,
> > Andrei.
> >
> >> -----Original Message-----
> >> From: Mandy Warren [mailto:mandys.inbox@gmail.com]
> >> Sent: Donnerstag, 19. September 2013 23:39
> >> To: users@cxf.apache.org
> >> Subject: Re: How to handle exceptions from a SOAP webservice in a
> >> single point
> >>
> >> Many thanks again for such a fast reply! So I have written an
> >> AbstractSoapInterceptor and managed to retrieve the exception from
> >> the message via
> >>
> >> Exchange exchange = message.getExchange;
> >>
> >> exchange.get(Exception.class);
> >>
> >> Pls can you explain how I then create a Fault with the info I want
> >> and send it back?
> >>
> >> Also because I want to use local transport I was looking at using
> >> SOAPHandlers instead of in interceptors to pull out some header info
> >> I need, is there a way to get / change the exception info from the
> >> handler instead of via an interceptor?
> >>
> >> Many thanks
> >>
> >> Sent from a mobile device
> >>
> >> On 19 Sep 2013, at 22:01, Sergey Beryozkin <sb...@gmail.com>
> wrote:
> >>
> >>> Hi
> >>> On 19/09/13 21:56, Mandy Warren wrote:
> >>>> Hi,
> >>>>
> >>>> I would like to handle all exceptions thrown by my webservice in a
> >>>> single point so I can create Faults with specific codes / strings
> >>>> based on info in the exception. When using JAX-RS I can do this
> >>>> very neatly via an Exception Mapper (and then convert to a REST
> >>>> response) but there doesn't seem to be an equivalent for JAX-WS..
> >>>>
> >>>> Is there a way to handle the exceptions before they are turned into
> >>>> SOAP Faults?
> >>> See
> >>>
> >>> http://cxf.547215.n5.nabble.com/Implementing-smarter-
> >> RuntimeException-
> >>> handling-for-JAX-WS-service-tt5734264.html
> >>>
> >>> HTH
> >>> Sergey
> >>>> Many thanks
> >>>> Mandy
> >>>
Re: How to handle exceptions from a SOAP webservice in a single point
Posted by Mandy Warren <ma...@gmail.com>.
Thanks Andrei,
just one thing, now I have the new fault how do I return it? The handleMessage(SoapMessage message) method returns a void so do I just throw the new SOAPFaultException or if not, how do I set the new fault in the message param?
Sent from a mobile device
On 20 Sep 2013, at 13:46, Andrei Shakirin <as...@talend.com> wrote:
> Hi,
>
> Regarding the first question you can do something like:
>
> fault = (SOAPFactory.newInstance()).createFault();
> fault.setFaultString(BUSINESS_FAULT_STRING);
> String errorString = businessException.getMessage();
> try {
> // 2. Parse exception string and add it as SOAPFault Details
> Document faultDoc = builder.parse(new ByteArrayInputStream(
> errorString.getBytes()));
> Detail detail = fault.addDetail();
> Document owner = detail.getOwnerDocument();
> Node details = owner.importNode(faultDoc.getDocumentElement(), true);
> detail.appendChild(details);
> } catch (SAXException e) {
> // 3. If exception string is not XML, set is as fault string
> fault.setFaultString(errorString);
> }
> return new SOAPFaultException(fault);
>
>
> Regards,
> Andrei.
>
>> -----Original Message-----
>> From: Mandy Warren [mailto:mandys.inbox@gmail.com]
>> Sent: Donnerstag, 19. September 2013 23:39
>> To: users@cxf.apache.org
>> Subject: Re: How to handle exceptions from a SOAP webservice in a single
>> point
>>
>> Many thanks again for such a fast reply! So I have written an
>> AbstractSoapInterceptor and managed to retrieve the exception from the
>> message via
>>
>> Exchange exchange = message.getExchange;
>>
>> exchange.get(Exception.class);
>>
>> Pls can you explain how I then create a Fault with the info I want and send it
>> back?
>>
>> Also because I want to use local transport I was looking at using
>> SOAPHandlers instead of in interceptors to pull out some header info I need,
>> is there a way to get / change the exception info from the handler instead of
>> via an interceptor?
>>
>> Many thanks
>>
>> Sent from a mobile device
>>
>> On 19 Sep 2013, at 22:01, Sergey Beryozkin <sb...@gmail.com> wrote:
>>
>>> Hi
>>> On 19/09/13 21:56, Mandy Warren wrote:
>>>> Hi,
>>>>
>>>> I would like to handle all exceptions thrown by my webservice in a
>>>> single point so I can create Faults with specific codes / strings
>>>> based on info in the exception. When using JAX-RS I can do this very
>>>> neatly via an Exception Mapper (and then convert to a REST response)
>>>> but there doesn't seem to be an equivalent for JAX-WS..
>>>>
>>>> Is there a way to handle the exceptions before they are turned into
>>>> SOAP Faults?
>>> See
>>>
>>> http://cxf.547215.n5.nabble.com/Implementing-smarter-
>> RuntimeException-
>>> handling-for-JAX-WS-service-tt5734264.html
>>>
>>> HTH
>>> Sergey
>>>> Many thanks
>>>> Mandy
>>>
RE: How to handle exceptions from a SOAP webservice in a single
point
Posted by Andrei Shakirin <as...@talend.com>.
Hi,
Regarding the first question you can do something like:
fault = (SOAPFactory.newInstance()).createFault();
fault.setFaultString(BUSINESS_FAULT_STRING);
String errorString = businessException.getMessage();
try {
// 2. Parse exception string and add it as SOAPFault Details
Document faultDoc = builder.parse(new ByteArrayInputStream(
errorString.getBytes()));
Detail detail = fault.addDetail();
Document owner = detail.getOwnerDocument();
Node details = owner.importNode(faultDoc.getDocumentElement(), true);
detail.appendChild(details);
} catch (SAXException e) {
// 3. If exception string is not XML, set is as fault string
fault.setFaultString(errorString);
}
return new SOAPFaultException(fault);
Regards,
Andrei.
> -----Original Message-----
> From: Mandy Warren [mailto:mandys.inbox@gmail.com]
> Sent: Donnerstag, 19. September 2013 23:39
> To: users@cxf.apache.org
> Subject: Re: How to handle exceptions from a SOAP webservice in a single
> point
>
> Many thanks again for such a fast reply! So I have written an
> AbstractSoapInterceptor and managed to retrieve the exception from the
> message via
>
> Exchange exchange = message.getExchange;
>
> exchange.get(Exception.class);
>
> Pls can you explain how I then create a Fault with the info I want and send it
> back?
>
> Also because I want to use local transport I was looking at using
> SOAPHandlers instead of in interceptors to pull out some header info I need,
> is there a way to get / change the exception info from the handler instead of
> via an interceptor?
>
> Many thanks
>
> Sent from a mobile device
>
> On 19 Sep 2013, at 22:01, Sergey Beryozkin <sb...@gmail.com> wrote:
>
> > Hi
> > On 19/09/13 21:56, Mandy Warren wrote:
> >> Hi,
> >>
> >> I would like to handle all exceptions thrown by my webservice in a
> >> single point so I can create Faults with specific codes / strings
> >> based on info in the exception. When using JAX-RS I can do this very
> >> neatly via an Exception Mapper (and then convert to a REST response)
> >> but there doesn't seem to be an equivalent for JAX-WS..
> >>
> >> Is there a way to handle the exceptions before they are turned into
> >> SOAP Faults?
> > See
> >
> > http://cxf.547215.n5.nabble.com/Implementing-smarter-
> RuntimeException-
> > handling-for-JAX-WS-service-tt5734264.html
> >
> > HTH
> > Sergey
> >> Many thanks
> >> Mandy
> >
Re: How to handle exceptions from a SOAP webservice in a single point
Posted by Mandy Warren <ma...@gmail.com>.
Many thanks again for such a fast reply! So I have written an AbstractSoapInterceptor and managed to retrieve the exception from the message via
Exchange exchange = message.getExchange;
exchange.get(Exception.class);
Pls can you explain how I then create a Fault with the info I want and send it back?
Also because I want to use local transport I was looking at using SOAPHandlers instead of in interceptors to pull out some header info I need, is there a way to get / change the exception info from the handler instead of via an interceptor?
Many thanks
Sent from a mobile device
On 19 Sep 2013, at 22:01, Sergey Beryozkin <sb...@gmail.com> wrote:
> Hi
> On 19/09/13 21:56, Mandy Warren wrote:
>> Hi,
>>
>> I would like to handle all exceptions thrown by my webservice in a single
>> point so I can create Faults with specific codes / strings based on info in
>> the exception. When using JAX-RS I can do this very neatly via an Exception
>> Mapper (and then convert to a REST response) but there doesn't seem to be
>> an equivalent for JAX-WS..
>>
>> Is there a way to handle the exceptions before they are turned into SOAP
>> Faults?
> See
>
> http://cxf.547215.n5.nabble.com/Implementing-smarter-RuntimeException-handling-for-JAX-WS-service-tt5734264.html
>
> HTH
> Sergey
>> Many thanks
>> Mandy
>
Re: How to handle exceptions from a SOAP webservice in a single point
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 19/09/13 21:56, Mandy Warren wrote:
> Hi,
>
> I would like to handle all exceptions thrown by my webservice in a single
> point so I can create Faults with specific codes / strings based on info in
> the exception. When using JAX-RS I can do this very neatly via an Exception
> Mapper (and then convert to a REST response) but there doesn't seem to be
> an equivalent for JAX-WS..
>
> Is there a way to handle the exceptions before they are turned into SOAP
> Faults?
>
See
http://cxf.547215.n5.nabble.com/Implementing-smarter-RuntimeException-handling-for-JAX-WS-service-tt5734264.html
HTH
Sergey
> Many thanks
> Mandy
>