Cant find document by id when running scheduled code

0 votes

Hi there Community,

I have the next problem:

I'm trying to scheduled a custom code to run every week. Its easy to make the scheduling and thats nice.

I've download the last code sample for custom codes, when I modify the code to run a search over a document inside a collection, I get some errors, before I tried findAllDocuments and gave me error too.

Here is the code:

String dbName = "TESTDB";  

String collectionName = "InfoCollection";

String docID = "53864b14e4b0b33395a6a7cf";  

logger.debug("Find Document by id in ["+dbName+"]["+collectionName+"] with ["+docID+"] NOW!", moduleName);

try

{

Storage storage = storageService.findDocumentById(dbName,collectionName,docID);

logger.debug("dbName is " + storage.getDbName(), moduleName);

//other code never reached here

}

catch(App42Exception exception)   

{  

    int appErrorCode = exception.getAppErrorCode();  

    int httpErrorCode = exception.getHttpErrorCode();

    logger.debug("Error finding document with id appCode["+appErrorCode+"] httpCode["+httpErrorCode+"]", moduleName);

    logger.debug("Cant find document with id ["+docID+"]", moduleName);

}

In the log service I've got the next messages:

"Error finding document with id appCode[0] httpCode[0]"

"Cant find document with id [53864b14e4b0b33395a6a7cf]"

I've triple checked the doc id, and as I said before even findAllDocuments fails.

Any idea where I could search for the answer?

 

Hope you can help me.

Thanks in advance.

 

 

 

asked May 30, 2014 in App42PaaS & BPaaS by chrystiansilva (33 points)

1 Answer

+1 vote
 
Best answer
Since you are getting appError code/http error code as 0, it means exception is something else and not NOT FOUND exception.

Can you please put exception.getMessage() in the log and see what is the exception there?
answered May 30, 2014 by ajay123 (899 points)
selected Jun 2, 2014 by chrystiansilva
Sure the resulting message is the next:

On findAllDocuments
"Error finding all document appCode[0] httpCode[0] Message[java.lang.IllegalArgumentException: Empty key]"

On findDocumentById
"Error finding document with id appCode[0] httpCode[0]  Message[java.lang.IllegalArgumentException: Empty key]"

Its the same error for both =/
Can you first run it locally it see what is the exception coming in? It looks like some initialization is missing out. Please put exception.printStackTrace() in catch block while running it locally.
Hi, I've ruined it locally:

This is the code on the catch part:

   exception.printStackTrace();
   int appErrorCode = exception.getAppErrorCode();  
   int httpErrorCode = exception.getHttpErrorCode();
   System.out.println("Error finding document with id appCode["+appErrorCode+"] httpCode["+httpErrorCode+"]  Message["+exception.getMessage()+"] Cause["+exception.getCause().getMessage()+"]");
   System.out.println("Cant find document with id ["+docID+"]");
   for(int i = 0; i < exception.getStackTrace().length; i++)
   {
    System.out.println("StackTrace element["+i+"] = ["+exception.getStackTrace()     [i].toString()+"]");
   }


And the console result was this:

Recieved Request Body : {"Studio":"Fk"}
StorageService created [true]
Find Document by id in [TestDB][InfoCollection] with [53864b14e4b0b33395a6a7cf] NOW!
com.shephertz.app42.paas.sdk.java.App42Exception: java.lang.IllegalArgumentException: Empty key
Error finding document with id appCode[0] httpCode[0]  Message[java.lang.IllegalArgumentException: Empty key] Cause[Empty key]
Cant find document with id [53864b14e4b0b33395a6a7cf]
StackTrace element[0] = [com.shephertz.app42.paas.sdk.java.storage.StorageService.findDocumentById(StorageService.java:602)]
StackTrace element[1] = [com.shephertz.app42.paas.customcode.sample.MyCustomCode.execute(MyCustomCode.java:68)]
StackTrace element[2] = [com.shephertz.app42.paas.customcode.sample.TestMyCustomCode.testMyCode(TestMyCustomCode.java:32)]
StackTrace element[3] = [com.shephertz.app42.paas.customcode.sample.TestMyCustomCode.main(TestMyCustomCode.java:40)]
Running Scheduled Custom Code
    at com.shephertz.app42.paas.sdk.java.storage.StorageService.findDocumentById(StorageService.java:602)
    at com.shephertz.app42.paas.customcode.sample.MyCustomCode.execute(MyCustomCode.java:68)
    at com.shephertz.app42.paas.customcode.sample.TestMyCustomCode.testMyCode(TestMyCustomCode.java:32)
    at com.shephertz.app42.paas.customcode.sample.TestMyCustomCode.main(TestMyCustomCode.java:40)
Caused by: java.lang.IllegalArgumentException: Empty key
    at javax.crypto.spec.SecretKeySpec.<init>(DashoA13*..)
    at com.shephertz.app42.paas.sdk.java.util.Util.computeHmac(Util.java:109)
    at com.shephertz.app42.paas.sdk.java.util.Util.sign(Util.java:88)
    at com.shephertz.app42.paas.sdk.java.storage.StorageService.findDocumentById(StorageService.java:591)
    ... 3 more

Is it useful to you? Im not a java developer and since a lot of years I have not touch it at all, so if I'm doing something wrong please tell me.

Thanks
OK, Looks like api key/secret key  has not been passed in the initialization. Can you put your initialization code here? Please make sure that service api reference has been properly initialized with API Key/Secret Key
Sure the initialisation code is just as in the sample, its the next:

public class MyCustomCode implements Executor
{
    private ServiceAPI sp = new ServiceAPI(         
                 "9b45a57f861c7d6356cfbdc2f82195eb8efb15bdb651b86dd5a285d4d8800619",
                  "0e5e86ab65daf09e219efae4e323e3cafe3b09710d88460df6f66a4a314fad2f");   
      
    private final int HTTP_STATUS_SUCCESS = 200;
    private String moduleName = "App42ScheduledCodeTest";

    @Override
    public HttpResponseObject execute(HttpRequestObject request)
    {
        JSONObject body = request.getBody();
        // Create JSON Response Based on Your business logic
        JSONObject jsonResponse = new JSONObject();
               
        System.out.println(" Recieved Request Body : " + body.toString());
moduleName);

        // Write Your Custom Code Here
        StorageService storageService = App42API.buildStorageService();
        System.out.println("StorageService created ["+(storageService != null)+"]");

The last line prints true.

Is it something wrong?
Instead of StorageService storageService = App42API.buildStorageService();
use StorageService storageService = sp.buildStorageService(); and it should be fine.
Yep, that solved it! Thanks!
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com