cancel
Showing results for 
Search instead for 
Did you mean: 

Integrity error handling in web scripts

schambon
Champ in-the-making
Champ in-the-making
Hi all,

Suppose that you have a custom model that defines an integrity constraint (eg you're testing that a file name fits the file name regexp). Suppose that you write a data web script (which takes json as input and outputs json as well) that lets you create nodes of your custom type. Now suppose that you want your web script to handle error cases gracefully, i.e. if the integrity constraint fails you want the script to return a 400 error code and push a nice explanatory message in the json return template. How would you go about doing this?

I'm finding that at the web script level there is no way to get the integrity error, because apparently the integrity checker only runs when the transaction is committed… which occurs after the web script returns. This means that I get some nasty messages back from my script, like this:

{
   "result" : "Node created",
   "nodeRef":"workspace://SpacesStore/edfaddde-afd1-4e2f-9fdb-f8be85179c5a",
   "name":"wrong? file: name!"
}<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <title>Web Script Status 500 - Internal Error</title>
      <link rel="stylesheet" href="/alfresco/css/base.css" type="text/css" />
   </head>
   <body>
      <div>
         <table>
            <tr>
               <td><img src="/alfresco/images/logo/AlfrescoLogo32.png" alt="Alfresco" /></td>
               <td><span class="title">Web Script Status 500 - Internal Error</span></td>
            </tr>
         </table>
         <br/>
         <table>
            <tr><td>The Web Script <a href="/alfresco/service/test/create?alf_ticket=TICKET_e3f29349013fcedc2c2a92f7e862b08c9a2c3f7d">/alfresco/service/test/create</a> has responded with a status of 500 - Internal Error.</td></tr>
         </table>
         <br/>
         <table>
            <tr><td><b>500 Description:</b></td><td> An error inside the HTTP server which prevented it from fulfilling the request.</td></tr>
            <tr><td> </td></tr>
            <tr><td><b>Message:</b></td><td>Found 1 integrity violations:

Etc. Also, the http status code is 200… which is wrong.

Note that the controller script does do something like:


   try {
      var node = companyhome.createNode(name, "my:customtype", props,   "cm:contains");
      model.node = node;
   } catch (error) {
      status.code = 500;
      status.message="Unexpected error";
      model.error = error.message;
      status.redirect=true;
      return;
   }

But that doesn't help, since no exceptions are raised at this stage. I'm not very keen on writing a java-backed script and handling transactions manually, but right now I don't see what else I can do, other than pre-checking the regexp in javascript before invoking createNode (which would mean repeating myself; the integrity constraint at the model level should be sufficient in an ideal world).

Note: I'm running 3.0.1 Enterprise.

Cheers,
Sylvain.
4 REPLIES 4

rogier_oudshoor
Champ in-the-making
Champ in-the-making
Integrity checks are performed when a transaction commits. To trigger these checks from within a webscript, you have to:
1. Ensure that the webscript itself is not a transaction (you can configure this in your desc.xml)
2. Create a Java-backed webscript
3. In your webscript, create a new transaction and attempt to commit it
4. Now, you can catch the error and process accordingly, possibly using your own custom 5xx error codes & ftl's

Gluck,

Rogier

hyperation
Champ on-the-rise
Champ on-the-rise
Integrity checks are performed when a transaction commits. To trigger these checks from within a webscript, you have to:
1. Ensure that the webscript itself is not a transaction (you can configure this in your desc.xml)
2. Create a Java-backed webscript
3. In your webscript, create a new transaction and attempt to commit it
4. Now, you can catch the error and process accordingly, possibly using your own custom 5xx error codes & ftl's

Gluck,

Rogier

Can you please provide some example for the transaction and attempt to commit step?

Thanks
Smiley Happy

schambon
Champ in-the-making
Champ in-the-making
Can you please provide some example for the transaction and attempt to commit step?

Quoting from memory, it should be something like:

UserTransaction tx = transactionService.getUserTransaction(false);
tx.begin();

// do something

try {
   tx.commit();
} catch (RollbackException re) {
   // something bad has happened and the transaction has been rolled back
   // notify the client
}

Hope that helps,
Sylvain.

hyperation
Champ on-the-rise
Champ on-the-rise
Thanks schambon, it works!

Smiley Happy