on 05-03-2013 8:03 PM
Hi, How can i catch DB exceptions like Unique Constraint errors in XSJS and put it in response object? Thanks, Naresh
Hi Naresh,
Maybe something like this
var conn = $.db.getConnection();
var pstmt = null;
var code = 0;
function closeresource(r) {
if (r) {
try {
r.close();
} catch (e) {
//ignore
}
}
}
function getStatusCode(msg) {
var m = msg.match('\\:\\s(\\d+)\\s\\-');
return (m) ? parseInt(m[1], 10) : null;
}
try {
pstmt = conn.prepareStatement('INSERT INTO "TABLENAME" ("SOMEFIELD") VALUES (?)');
pstmt.setString(1, '1');
pstmt.executeUpdate();
$.response.setBody('OK');
$.response.status = $.net.http.OK;
} catch (e) {
code = getStatusCode(e.message);
if (code && code === 301) {
$.response.setBody('unique constraint violated');
} else {
$.response.setBody('something else');
}
$.response.status = $.net.http.BAD_REQUEST;
} finally {
closeresource(pstmt);
closeresource(conn);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
78 | |
9 | |
9 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.