You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@olingo.apache.org by anurag gujral <an...@gmail.com> on 2016/05/31 22:55:27 UTC

custom exception handling in olingo

Hi All,
I am developing OData APIs, as part of the implementation if some errors
occurs during processing of an API request, I am throwing exception of type
MyCustomException.
I want to do some custom handling when exception of type MyCustomException
is thrown.
I looked at code for apache-olingo 4.0 implementation I see the following
function:

 public ODataResponse process(final ODataRequest request) {
    ODataResponse response = new ODataResponse();
    final int responseHandle =
debugger.startRuntimeMeasurement("ODataHandler", "process");
    try {
      processInternal(request, response);
    } catch (final UriValidationException e) {
      ODataServerError serverError =
ODataExceptionHelper.createServerErrorObject(e, null);
      handleException(request, response, serverError, e);
    } catch (final UriParserSemanticException e) {
      ODataServerError serverError =
ODataExceptionHelper.createServerErrorObject(e, null);
      handleException(request, response, serverError, e);
    } catch (final UriParserSyntaxException e) {
      ODataServerError serverError =
ODataExceptionHelper.createServerErrorObject(e, null);
      handleException(request, response, serverError, e);
    } catch (final UriParserException e) {
      ODataServerError serverError =
ODataExceptionHelper.createServerErrorObject(e, null);
      handleException(request, response, serverError, e);
    } catch (ContentNegotiatorException e) {
      ODataServerError serverError =
ODataExceptionHelper.createServerErrorObject(e, null);
      handleException(request, response, serverError, e);
    } catch (SerializerException e) {
      ODataServerError serverError =
ODataExceptionHelper.createServerErrorObject(e, null);
      handleException(request, response, serverError, e);
    } catch (DeserializerException e) {
      ODataServerError serverError =
ODataExceptionHelper.createServerErrorObject(e, null);
      handleException(request, response, serverError, e);
    } catch (PreconditionException e) {
      ODataServerError serverError =
ODataExceptionHelper.createServerErrorObject(e, null);
      handleException(request, response, serverError, e);
    } catch (ODataHandlerException e) {
      ODataServerError serverError =
ODataExceptionHelper.createServerErrorObject(e, null);
      handleException(request, response, serverError, e);
    } catch (ODataApplicationException e) {
      ODataServerError serverError =
ODataExceptionHelper.createServerErrorObject(e);
      handleException(request, response, serverError, e);
    } catch (Exception e) {
      ODataServerError serverError =
ODataExceptionHelper.createServerErrorObject(e);
      handleException(request, response, serverError, e);
    }
    debugger.stopRuntimeMeasurement(responseHandle);
    return response;
  }

This process function(which is part of core onlingo code) eats up any
exception thus leaving no room for custom handling unless I change olingo
code.
Please guide how I can handle custom handling to handle my own  custom
exception?
Thanks & Regards,
Anurag

Re: custom exception handling in olingo

Posted by mibo <mi...@apache.org>.
Hi Anurag,


In Olingo (V4.2.x) you can register your own „ErrorProcessor“ at the „ODataHandler“ (code snippet below).
Afterwards your registered handler is called for each catched exception in the try/catch of the „process“ method.


Best Regards, Michael


      ODataHandler handler = odata.createHandler(serviceMetadata);
      handler.register(new ErrorProcessor() {
        @Override
        public void processError(ODataRequest request, ODataResponse response, ODataServerError serverError, ContentType responseFormat) {
          // do error handling 
        }


        @Override
        public void init(OData odata, ServiceMetadata serviceMetadata) {
          // for initialization
        }
      });










> On Jun 1, 2016, at 12:55 AM, anurag gujral <an...@gmail.com> wrote:
> 
> 
> Hi All,I am developing OData APIs, as part of the implementation if some errors occurs during processing of an API request, I am throwing exception of type MyCustomException.
> I want to do some custom handling when exception of type MyCustomException is thrown.
> I looked at code for apache-olingo 4.0 implementation I see the following function:
> 
> 
>  public ODataResponse process(final ODataRequest request) {
>     ODataResponse response = new ODataResponse();
>     final int responseHandle = debugger.startRuntimeMeasurement("ODataHandler", "process");
>     try {
>       processInternal(request, response);
>     } catch (final UriValidationException e) {
>       ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
>       handleException(request, response, serverError, e);
>     } catch (final UriParserSemanticException e) {
>       ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
>       handleException(request, response, serverError, e);
>     } catch (final UriParserSyntaxException e) {
>       ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
>       handleException(request, response, serverError, e);
>     } catch (final UriParserException e) {
>       ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
>       handleException(request, response, serverError, e);
>     } catch (ContentNegotiatorException e) {
>       ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
>       handleException(request, response, serverError, e);
>     } catch (SerializerException e) {
>       ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
>       handleException(request, response, serverError, e);
>     } catch (DeserializerException e) {
>       ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
>       handleException(request, response, serverError, e);
>     } catch (PreconditionException e) {
>       ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
>       handleException(request, response, serverError, e);
>     } catch (ODataHandlerException e) {
>       ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
>       handleException(request, response, serverError, e);
>     } catch (ODataApplicationException e) {
>       ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e);
>       handleException(request, response, serverError, e);
>     } catch (Exception e) {
>       ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e);
>       handleException(request, response, serverError, e);
>     }
>     debugger.stopRuntimeMeasurement(responseHandle);
>     return response;
>   }
> 
> 
> 
> This process function(which is part of core onlingo code) eats up any exception thus leaving no room for custom handling unless I change olingo code.
> Please guide how I can handle custom handling to handle my own  custom exception?
> Thanks & Regards,
> Anurag
> 

Re: custom exception handling in olingo

Posted by Ramesh Reddy <ra...@redhat.com>.
If you use ServiceHandler based service implementation there is callback function for introspecting the any exception thrown including the custom exceptions. For example implementation look at TripPinServiceTest.java class. 

----- Original Message -----

> Hi All,
> I am developing OData APIs, as part of the implementation if some errors
> occurs during processing of an API request, I am throwing exception of type
> MyCustomException.
> I want to do some custom handling when exception of type MyCustomException is
> thrown.
> I looked at code for apache-olingo 4.0 implementation I see the following
> function:

> public ODataResponse process(final ODataRequest request) {
> ODataResponse response = new ODataResponse();
> final int responseHandle = debugger.startRuntimeMeasurement("ODataHandler",
> "process");
> try {
> processInternal(request, response);
> } catch (final UriValidationException e) {
> ODataServerError serverError =
> ODataExceptionHelper.createServerErrorObject(e, null);
> handleException(request, response, serverError, e);
> } catch (final UriParserSemanticException e) {
> ODataServerError serverError =
> ODataExceptionHelper.createServerErrorObject(e, null);
> handleException(request, response, serverError, e);
> } catch (final UriParserSyntaxException e) {
> ODataServerError serverError =
> ODataExceptionHelper.createServerErrorObject(e, null);
> handleException(request, response, serverError, e);
> } catch (final UriParserException e) {
> ODataServerError serverError =
> ODataExceptionHelper.createServerErrorObject(e, null);
> handleException(request, response, serverError, e);
> } catch (ContentNegotiatorException e) {
> ODataServerError serverError =
> ODataExceptionHelper.createServerErrorObject(e, null);
> handleException(request, response, serverError, e);
> } catch (SerializerException e) {
> ODataServerError serverError =
> ODataExceptionHelper.createServerErrorObject(e, null);
> handleException(request, response, serverError, e);
> } catch (DeserializerException e) {
> ODataServerError serverError =
> ODataExceptionHelper.createServerErrorObject(e, null);
> handleException(request, response, serverError, e);
> } catch (PreconditionException e) {
> ODataServerError serverError =
> ODataExceptionHelper.createServerErrorObject(e, null);
> handleException(request, response, serverError, e);
> } catch (ODataHandlerException e) {
> ODataServerError serverError =
> ODataExceptionHelper.createServerErrorObject(e, null);
> handleException(request, response, serverError, e);
> } catch (ODataApplicationException e) {
> ODataServerError serverError =
> ODataExceptionHelper.createServerErrorObject(e);
> handleException(request, response, serverError, e);
> } catch (Exception e) {
> ODataServerError serverError =
> ODataExceptionHelper.createServerErrorObject(e);
> handleException(request, response, serverError, e);
> }
> debugger.stopRuntimeMeasurement(responseHandle);
> return response;
> }

> This process function(which is part of core onlingo code) eats up any
> exception thus leaving no room for custom handling unless I change olingo
> code.
> Please guide how I can handle custom handling to handle my own custom
> exception?
> Thanks & Regards,
> Anurag