cancel
Showing results for 
Search instead for 
Did you mean: 

What to return from GetEntity if requested record is not found?

frankluzat
Participant
0 Kudos

Hi,

In my implementation of the GetEntity method I select the requested record from the database according to the key values passed to the method.

In case the requested record could not be selected because it doesn't exist, I simply return the empty (initial) er_entity.

This works in most cases and leads to an error like this:

       "errordetails" : [

         {

           "code" : "/IWBEP/CX_MGW_BUSI_EXCEPTION",

           "message" : "Resource not found for segment 'ContractVersion'",

           "propertyref" : "",

           "severity" : "error",

           "target" : ""

         }

       ]

In case my entity has fields of type Edm.DateTime which are mapped to ABAP type TIMESTAMP then I will receive an error like this:

   "error" : {

     "code" : "0050569259751EE4BA9710043F8A5115",

     "message" : {

       "lang" : "en",

       "value" : "In the context of Data Services an unknown internal server error occurred"

     }

In the Gateway error log the error info is "Invalid format (return structure): Property Name 'ChDateTime',  Property Value '0 '"

So what shell be done if the requested entity is not found?

Should I manually raise an exception in the getEntity method?

Any other proposals or best practice?

Accepted Solutions (1)

Accepted Solutions (1)

AshwinDutt
Active Contributor
0 Kudos

Hello Frank,

IMO don't need to do any exception handling when no data found and GW result is initial. its fine if GW returns null response when no data found.

Since the error is pointing to the Property Date field, please have a look at the below which explains handling of Date and Time fields of the GW model.

Above will help to fix the Date and Time related issues.

There are many others discussions in the forum for Date and Time handling. So please check for other threads.

Coming to the error ' Resource not found for segment 'ContractVersion ' i suspect that your URL is pointing to a resource which is not defined in your GW Model.

Please note the properties which we use in URL are case sensitive.

Please share the URL which your are using for which error Resource not found for segment 'ContractVersion is encountered.

When this error is found ? after Get_Entity method is executed ? or its a 400 bad request encountered at the initial stage of firing GET service ?

Regards,

Ashwin

frankluzat
Participant
0 Kudos

Hi Ashwin,

thanks for your reply.

I tested it again with my ContractVersionSet and when I return the empty er_entity it is unbound by the GW framework and the /IWBEP/CX_MGW_BUSI_EXCEPTION is raised with the "Resource not found" message which seems to be by design.

This happens when this is processed as single GetEntity request.



When this GET request is contained in a $batch call, then the return is like


"In the context of Data Services an unknown internal server error occurred".


This is the batch:


--batch_79e3-b773-66f2

content-type: application/http

content-transfer-encoding: binary

GET ContractSet('1')/ContractVersions?$skip=0&$top=100&$orderby=ChDateTime%20desc&$expand=User&$inlinecount=allpages HTTP/1.1

Accept: application/json

Content-Type: application/json

Accept-Language: en

DataServiceVersion: 2.0

MaxDataServiceVersion: 2.0

x-csrf-token: mydfvtrB0sJkyu0Oze-0zQ==

--batch_79e3-b773-66f2

content-type: application/http

content-transfer-encoding: binary

GET ContractVersionSet(ContractId='1',ChDateTime=datetime'2012-05-04T05%3A27%3A09')?$expand=ContractType HTTP/1.1

Accept: application/json

Content-Type: application/json

Accept-Language: en

DataServiceVersion: 2.0

MaxDataServiceVersion: 2.0

x-csrf-token: mydfvtrB0sJkyu0Oze-0zQ==

--batch_79e3-b773-66f2--

The second request in the batch returns the "empty" entity because it was not found.

This is the response:


{

   "error" : {

     "code" : "0050569259751EE4BA9710043F8A5115",

     "message" : {

       "lang" : "en",

       "value" : "In the context of Data Services an unknown internal server error occurred"

     },

     "innererror" : {

       "transactionid" : "55F7F761990A1460E10080000A010791",

       "timestamp" : "20150917141027.3531790",

       "Error_Resolution" : {

         "SAP_Transaction" : "Run transaction /IWFND/ERROR_LOG on SAP NW Gateway hub system and search for entries with the timestamp above for more details",

         "SAP_Note" : "See SAP Note 1797736 for error analysis (https://service.sap.com/sap/support/notes/1797736)"

       }

     }

   }

}

This is the error log:

This is not good. I regularly scan the gateway log for errors to reveal errors in the implementation of the OData services.
If there are "resource not found" errors, ok, but if there are "Invalid format" errors I have to investigate which is lost time in that case.

Any ideas?

AshwinDutt
Active Contributor
0 Kudos

Hello Frank,

Can you share the screen shot of your GW model for the Entities ContractSet and ContractVersionSet ?

and also the code written for these inside data Provider Class ?

Regards,

Ashwin

frankluzat
Participant
0 Kudos

Sorry, can't share any code.

Answers (1)

Answers (1)

Former Member
0 Kudos

Hello Frank,

This seems to be fairly simple.

1. Close the GW client.

2. Go to the Date Property in your entity type.

3. Check the nullable checkbox for the property.

4. As a safety measure, do this for all properties except the key properties.

5. Regenerate the runtime artifacts.

6. Clear the cache using transaction /IWFND/CACHE_CLEANUP.

7. Run the service again.

Hope this helps.

Cheers,

-NK

frankluzat
Participant
0 Kudos

The datetime field cannot be nullable because it's a key field of the entity!

Former Member
0 Kudos

Hi Frank,

The other option is you change the property at the ABAP level.

Try something like this-

Entity Type ->your Date Property ->Select Abap Editor.

Cheers,

-NK

frankluzat
Participant
0 Kudos

Hi Nimish,

this is not possible too because I need the date and time!

So it has to be TIMESTAMP!

Former Member
0 Kudos

Hi Frank,

We'll get a clearer idea by seeing your code in data provider class.

If you cannot share it, try adding something like this in the code-

If date field from Database is empty.

     set date field = 0.

endif.

Let me know if this works.

Cheers,

-NK

frankluzat
Participant
0 Kudos

Hi Nimish,

since my select failes - entity with provided key values not found -  ls_contrvers is initial:

Field CHDATETIME is the timestamp field, which is also key of the entity (besides CONTRACTID)

So the IF block is not entered and er_entity is left untouched.

As you can see, the timestamp field has its initial value. Moving 0 to this field wouldn't make any difference.

Any other ideas?

Former Member
0 Kudos

Hi Frank,

Another wild suggestion would be to pass some default value to the date field if nothing comes from the database. If you can, try that.

Apart from that, i cannot suggest anything else.

Regards,

-NK

AshwinDutt
Active Contributor
0 Kudos

Hello Frank,

As i can understand from you that these are Key Fields and cannot be made null-able in the GW Model.

When no data fetched by the Query, initial values being loaded into the GW response structure and it fails.

So i would suggest the below and opt for this :

When no data found raise an exception with a meaning full message using IWBEP/IF_MESSAGE_CONTAINER.

/IWBEP/IF_MESSAGE_CONTAINER - SAP Gateway Foundation (SAP_GWFND) - SAP Library

Regards,

Ashwin

frankluzat
Participant
0 Kudos

Since it works when not in a $batch operation I think this could probably be a Bug in NW Gateway?

AshwinDutt
Active Contributor
0 Kudos

Hello Frank,

Not sure about this behavior which you are mentioning as i personally never encountered such cases.

Ideally irrespective if its inside batch request or a normal get request, when Date & Time not made nullable we get error.

One thing which i have always observed is for some reason if Date & Time are not marked as nullable and on data found case initial values are loaded into the final response structure of GW then it fails to parse those initial values.

Regards,

Ashwin

Former Member
0 Kudos

Hi Frank,

Like Ashwin Dutt R pointed out, if the NW Gateway doesn't find anything in the datetime field, it loads initial values (if it has been made nullable).

If you don't make it nullable, the service stops and gives you an error.

If you can't make it nullable, pass a message of the context "please enter the date" using IWBEP/IF_MESSAGE_CONTAINER.

Apart from that, I have always been forced to make the datetime field nullable, for some reason.

Regards,

-NK

UPDATE-

Your key property is the datetime property, which means there will be a date property in your backend table from where you are getting the data (assumably; if not then the data that you are fetching will be erroneous).

This means that you will fetch only one record on that date (considering your date field is obviously unique since it is a key field).

So your date field cannot be null, as your key is not supposed to be null (both at the frontend as well as the backend).

This means that you shouldn't get the datetime nullable error over here, as the date that comes from your backend is not supposed to be empty.

Check your backend as well as frontend logic because this is not a SAP bug or a problem.

Regards-

-Nimish

frankluzat
Participant
0 Kudos

My OData service is accessed by a web appliction built with SAPUI5.

The keys of the entity which is to be displayed on a "details page" are passed as navigation parameters in the hash:

...myapp/#/contract/1/?ChDateTime=1442824953000

This parameters are taken to build the query string for the ContractVersion entity.

So it is possible that the user manipulates the URL with a datetime value that doesn't exist in the backend database.

So the case that an entity is requested which doesn't exist can but should not happen. In case it happens I expect the gateway to return a meaningful error message. Since this - in my oppinion - wrong behavior only happens when in a $batch operation, it doesn't make sense to me.

Btw: The CHDATETIME field is not the only key in this table. CONTRACTID is also a key field.

For now I think I have to live with it, but maybe I'll open an OSS report for this later.