Integrity error handling in web scripts

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2009 10:01 AM
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:
Etc. Also, the http status code is 200… which is wrong.
Note that the controller script does do something like:
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.
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.
Labels:
- Labels:
-
Archive
4 REPLIES 4
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2009 02:40 AM
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
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
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2010 05:43 PM
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


Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2010 03:26 AM
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 somethingtry { tx.commit();} catch (RollbackException re) { // something bad has happened and the transaction has been rolled back // notify the client}
Hope that helps,
Sylvain.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2010 04:53 PM
Thanks schambon, it works!

