AnsweredAssumed Answered

Integrity error handling in web scripts

Question asked by schambon on Aug 14, 2009
Latest reply on May 3, 2010 by hyperation
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.

Outcomes