# HG changeset patch # User Daniele Nicolodi # Date 1323104643 -3600 # Node ID 977eb37f31cbbe96d2f89d87a6eafadfc5e0e880 # Parent 11e3ed9d2115d6f3cecb5a752cafa3749834bf3c User friendlier errors from utils.mysql.connect diff -r 11e3ed9d2115 -r 977eb37f31cb m-toolbox/classes/+utils/@mysql/connect.m --- a/m-toolbox/classes/+utils/@mysql/connect.m Mon Dec 05 16:20:06 2011 +0100 +++ b/m-toolbox/classes/+utils/@mysql/connect.m Mon Dec 05 18:04:03 2011 +0100 @@ -5,11 +5,11 @@ % % conn = utils.mysql.connect(hostname, database, username, password) % -% This function returns a Java object implementing the java.sql.Connection -% interface connected to the given database using the provided credentials. -% If the connection fails because the given username and password pair is not -% accepted by the server an utils:mysql:connect:AccessDenied error is thrown. +% This function returns a Java java.sql.Connection object. % +% On authetication error an utils:mysql:connect:AccessDenied exception +% is thrown. On other errors an utils:mysql:connect:ConnectionError +% exception is thrown. % informative message import utils.const.* @@ -26,12 +26,24 @@ % connect conn = db.connect(uri, pl); catch ex - % haven't decided yet if this code should be here or higher in the stack + % exceptions handling in matlab sucks if strcmp(ex.identifier, 'MATLAB:Java:GenericException') - % exceptions handling in matlab sucks - if ~isempty(strfind(ex.message, 'java.sql.SQLException: Access denied')) - throw(MException('utils:mysql:connect:AccessDenied', '### access denied').addCause(ex)); + % extract exception class and message + lines = regexp(ex.message, '\n', 'split'); + p = strfind(lines{2}, ': '); + id = lines{2}(1:p(1)-1); + message = lines{2}(p(1)+2:end); + % some notable cases + switch id + case 'java.sql.SQLException' + throwAsCaller(MException('utils:mysql:connect:AccessDenied', '### access denied: %s', message)) + case 'com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException' + throwAsCaller(MException('utils:mysql:connect:ConnectionError', '### connection error: %s. check database name', message)) + case 'com.mysql.jdbc.exceptions.jdbc4.CommunicationsException' + throwAsCaller(MException('utils:mysql:connect:ConnectionError', '### connection error: %s. check hostname', message)) end + % user friendlier exception + throwAsCaller(MException('utils:mysql:connect:ConnectionError', '### connection error: %s: %s', id, message)) end rethrow(ex); end