1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
unit MyxError;
// Copyright (C) 2003, 2004 MySQL AB
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
interface
uses
gnugettext, Classes, SysUtils, Forms, AuxFuncs, myx_public_interface;
type
// Common base class for all MySQL exceptions.
EMyxError = class(Exception)
private
FDescription: WideString;
protected
function GetFormattedMessage: WideString; virtual;
public
constructor Create(ADescription: WideString); virtual;
property Description: WideString read FDescription;
property FormattedMessage: WideString read GetFormattedMessage;
end;
EMyxEnhancedError = class(EMyxError)
private
FErrorNr: Integer;
FErrorText: WideString;
protected
function GetFormattedMessage: WideString; override;
public
constructor Create(ADescription: WideString; AErrorNr: Integer; AErrorText: WideString); reintroduce; virtual;
property ErrorNr: Integer read FErrorNr;
property ErrorText: WideString read FErrorText;
end;
EMyxCriticalError = class(EMyxEnhancedError)
public
constructor Create(ADescription: WideString; ErrorNr: Integer); reintroduce; virtual;
end;
EMyxSystemError = class(EMyxEnhancedError)
protected
function GetFormattedMessage: WideString; override;
public
constructor Create(ADescription: WideString; ErrorNr: Integer); reintroduce; virtual;
end;
EMyxSQLError = class(EMyxEnhancedError)
protected
function GetFormattedMessage: WideString; override;
end;
EMyxLibraryError = class(EMyxEnhancedError)
private
FErrorNrParam: WideString;
protected
function GetFormattedMessage: WideString; override;
public
constructor Create(ADescription: WideString; AErrorNr: Integer; AErrorNrParam: WideString); reintroduce; virtual;
property ErrorNrParam: WideString read FErrorNrParam;
end;
EMyxCommonLibraryError = class(EMyxLibraryError)
public
constructor Create(ADescription: WideString; AErrorNr: Integer; AErrorNrParam: WideString); override;
end;
procedure ShowError(E: Exception);
//----------------------------------------------------------------------------------------------------------------------
implementation
//----------------- EMyxError- -----------------------------------------------------------------------------------------
constructor EMyxError.Create(ADescription: WideString);
begin
FDescription := ADescription;
// Set the base exception description as well, although due to the ANSI conversion some info might be lost.
Message := ADescription;
end;
//----------------------------------------------------------------------------------------------------------------------
function EMyxError.GetFormattedMessage: WideString;
// Returns a composed message about the exception. Descendants should override for more meaningfull output.
begin
Result := FDescription;
end;
//----------------- EMyxEnhancedError ----------------------------------------------------------------------------------
constructor EMyxEnhancedError.Create(ADescription: WideString; AErrorNr: Integer; AErrorText: WideString);
begin
inherited Create(ADescription);
FErrorNr := AErrorNr;
FErrorText := AErrorText;
end;
//----------------------------------------------------------------------------------------------------------------------
function EMyxEnhancedError.GetFormattedMessage: WideString;
begin
Result := WideFormat(_('%s'#10' The following error occured: %s (%d)'), [inherited GetFormattedMessage, FErrorText, FErrorNr]);
end;
//----------------- EMyxCriticalError ----------------------------------------------------------------------------------
constructor EMyxCriticalError.Create(ADescription: WideString; ErrorNr: Integer);
begin
inherited Create(ADescription, ErrorNr, '');
end;
//----------------- EMyxSystemError ------------------------------------------------------------------------------------
constructor EMyxSystemError.Create(ADescription: WideString; ErrorNr: Integer);
begin
inherited Create(ADescription, ErrorNr, SysErrorMessage(ErrorNr));
end;
//----------------------------------------------------------------------------------------------------------------------
function EMyxSystemError.GetFormattedMessage: WideString;
begin
Result := WideFormat(_('System Error Number %d'#10'%s'), [ErrorNr, ErrorText]);
end;
//----------------- EMyxSQLError ---------------------------------------------------------------------------------------
function EMyxSQLError.GetFormattedMessage: WideString;
begin
Result := WideFormat(_('A MySQL error was encountered. The message is:' + #10#10 + '%s'), [inherited GetFormattedMessage]);
end;
//----------------- EMyxLibraryError -----------------------------------------------------------------------------------
constructor EMyxLibraryError.Create(ADescription: WideString; AErrorNr: Integer; AErrorNrParam: WideString);
begin
inherited Create(ADescription, AErrorNr, AErrorNrParam);
end;
//----------------------------------------------------------------------------------------------------------------------
function EMyxLibraryError.GetFormattedMessage: WideString;
begin
Result := WideFormat(_('Library Error Number %d' + #13#10 + '%s'), [ErrorNr, ErrorNrParam]);
end;
//----------------- EMyxCommonLibraryError -----------------------------------------------------------------------------
constructor EMyxCommonLibraryError.Create(ADescription: WideString; AErrorNr: Integer; AErrorNrParam: WideString);
begin
case MYX_LIB_ERROR(FErrorNr) of
MYX_ERROR_CANT_OPEN_FILE:
ADescription := WideFormat(_('The File %s cannot be opened.'), [ErrorNrParam]);
MYX_ERROR_CANT_CONNECT_TO_INSTANCE:
ADescription := _('A connection to the server cannot be established.');
MYX_XML_PARSE_ERROR:
ADescription := WideFormat(_('An error occured while parsing the XML file %s.'), [ErrorNrParam]);
MYX_XML_NO_VALID_DOCUMENT:
ADescription := WideFormat(_('An error occured while validating the XML file %s.'), [ErrorNrParam]);
MYX_XML_EMPTY_DOCUMENT:
ADescription := WideFormat(_('The XML file %s is empty.'), [ErrorNrParam]);
MYX_SQL_ERROR:
ADescription := _('An SQL error occured.');
MYX_STOP_EXECUTION:
ADescription := _('The execution was stopped.');
end;
inherited;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure ShowError(E: Exception);
var
res: Integer;
MyxError: EMyxError;
Message: WideString;
begin
if not (Application.Terminated) then
begin
if E is EMyxError then
begin
MyxError := E as EmyxError;
Message := MyxError.FormattedMessage;
if (E is EMyxCriticalError) then
begin
res := ShowModalDialog(Application.Title + ' ' + _('Critical Error'),
Format(_('The following Exception occurred:' + #13#10 + '%s' + #13#10#13#10 +
'Please choose whether to continue %s, terminate the application ' +
'or restart the program.'), [Message, Application.Title]),
myx_mtError, _('Continue') + #13#10 + _('Terminate') + #13#10 + _('Restart'));
if (res = 2) then
Application.Terminate
else
if (res = 3) then
begin
CreateSubProcess(Application.ExeName);
Application.Terminate;
end;
end
else
if (E is EMyxSQLError) then
begin
ShowModalDialog(Application.Title + ' ' + _('SQL Error'), Message + #13#10#13#10 +
MyxError.FormattedMessage, myx_mtError, _('OK'));
end
else
if (E is EMyxSystemError) then
begin
ShowModalDialog(Application.Title + ' ' + _('System Error'), Message + #13#10#13#10 +
MyxError.FormattedMessage, myx_mtError, _('OK'));
end
else
if (E is EMyxLibraryError) then
begin
ShowModalDialog(Application.Title + ' ' + _('Library Error'), Message + #13#10#13#10 + MyxError.FormattedMessage,
myx_mtError, _('OK'));
end
else
begin
ShowModalDialog(Application.Title + ' Error', 'The following exception occurred:' + #13#10#13#10 +
Message, myx_mtError, 'OK');
end
end;
end
else
begin
ShowModalDialog(Application.Title + ' Error', 'The following Exception occurred:' + #13#10#13#10 + E.Message,
myx_mtError, 'OK');
end;
end;
//----------------------------------------------------------------------------------------------------------------------
end.
|