Comparison

util/sql.lua @ 7317:a2dce746599b

util.sql: Log errors in transaction to error level with traceback but return only error message (fixes #464)
author Kim Alvefur <zash@zash.se>
date Fri, 25 Mar 2016 16:09:34 +0100
parent 7312:b4e99602ae75
child 7428:f791651ee334
comparison
equal deleted inserted replaced
7315:4fd984d1e445 7317:a2dce746599b
200 engine.select = engine.execute_query; 200 engine.select = engine.execute_query;
201 engine.delete = engine.execute_update; 201 engine.delete = engine.execute_update;
202 engine.update = engine.execute_update; 202 engine.update = engine.execute_update;
203 end 203 end
204 end 204 end
205 local function handleerr(err)
206 log("error", "Error in SQL transaction: %s", debug_traceback(err, 3));
207 return err;
208 end
205 function engine:_transaction(func, ...) 209 function engine:_transaction(func, ...)
206 if not self.conn then 210 if not self.conn then
207 local ok, err = self:connect(); 211 local ok, err = self:connect();
208 if not ok then return ok, err; end 212 if not ok then return ok, err; end
209 end 213 end
210 --assert(not self.__transaction, "Recursive transactions not allowed"); 214 --assert(not self.__transaction, "Recursive transactions not allowed");
211 local args, n_args = {...}, select("#", ...); 215 local args, n_args = {...}, select("#", ...);
212 local function f() return func(unpack(args, 1, n_args)); end 216 local function f() return func(unpack(args, 1, n_args)); end
213 log("debug", "SQL transaction begin [%s]", tostring(func)); 217 log("debug", "SQL transaction begin [%s]", tostring(func));
214 self.__transaction = true; 218 self.__transaction = true;
215 local success, a, b, c = xpcall(f, debug_traceback); 219 local success, a, b, c = xpcall(f, handleerr);
216 self.__transaction = nil; 220 self.__transaction = nil;
217 if success then 221 if success then
218 log("debug", "SQL transaction success [%s]", tostring(func)); 222 log("debug", "SQL transaction success [%s]", tostring(func));
219 local ok, err = self.conn:commit(); 223 local ok, err = self.conn:commit();
220 if not ok then return ok, err; end -- commit failed 224 if not ok then return ok, err; end -- commit failed