Improve exceptions.

FossilOrigin-Name: 12b5ef483a1f9eae92df83cf8a6aded299ef10dd2972059a36c79d299e676a0d
This commit is contained in:
Jonathan Schleifer 2013-07-23 14:11:41 +00:00
parent 16dc91b0e7
commit 25c48048a1
8 changed files with 50 additions and 61 deletions

View file

@ -41,13 +41,11 @@
} }
if ((_connnection = PQconnectdb([connectionInfo UTF8String])) == NULL) if ((_connnection = PQconnectdb([connectionInfo UTF8String])) == NULL)
@throw [OFOutOfMemoryException @throw [OFOutOfMemoryException exception];
exceptionWithClass: [self class]];
if (PQstatus(_connnection) == CONNECTION_BAD) if (PQstatus(_connnection) == CONNECTION_BAD)
@throw [PGConnectionFailedException @throw [PGConnectionFailedException
exceptionWithClass: [self class] exceptionWithConnection: self];
connection: self];
[pool release]; [pool release];
} }
@ -72,9 +70,8 @@
if (PQresultStatus(result) == PGRES_FATAL_ERROR) { if (PQresultStatus(result) == PGRES_FATAL_ERROR) {
PQclear(result); PQclear(result);
@throw [PGCommandFailedException @throw [PGCommandFailedException
exceptionWithClass: [self class] exceptionWithConnection: self
connection: self command: command];
command: command];
} }
switch (PQresultStatus(result)) { switch (PQresultStatus(result)) {
@ -86,9 +83,8 @@
default: default:
PQclear(result); PQclear(result);
@throw [PGCommandFailedException @throw [PGCommandFailedException
exceptionWithClass: [self class] exceptionWithConnection: self
connection: self command: command];
command: command];
} }
} }
@ -115,15 +111,17 @@
if ([parameter isKindOfClass: [OFString class]]) if ([parameter isKindOfClass: [OFString class]])
values[i++] = [parameter UTF8String]; values[i++] = [parameter UTF8String];
else if ([parameter isKindOfClass: [OFNumber class]]) { else if ([parameter isKindOfClass: [OFNumber class]]) {
switch ([parameter type]) { OFNumber *number = parameter;
switch ([number type]) {
case OF_NUMBER_BOOL: case OF_NUMBER_BOOL:
if ([parameter boolValue]) if ([number boolValue])
values[i++] = "t"; values[i++] = "t";
else else
values[i++] = "f"; values[i++] = "f";
break; break;
default: default:
values[i++] = [[parameter description] values[i++] = [[number description]
UTF8String]; UTF8String];
break; break;
} }
@ -151,9 +149,8 @@
default: default:
PQclear(result); PQclear(result);
@throw [PGCommandFailedException @throw [PGCommandFailedException
exceptionWithClass: [self class] exceptionWithConnection: self
connection: self command: command];
command: command];
} }
} }
@ -214,9 +211,8 @@
if (PQresultStatus(result) != PGRES_COMMAND_OK) { if (PQresultStatus(result) != PGRES_COMMAND_OK) {
PQclear(result); PQclear(result);
@throw [PGCommandFailedException @throw [PGCommandFailedException
exceptionWithClass: [self class] exceptionWithConnection: self
connection: self command: command];
command: command];
} }
PQclear(result); PQclear(result);

View file

@ -32,8 +32,7 @@
- (id)objectAtIndex: (size_t)index - (id)objectAtIndex: (size_t)index
{ {
if (index > PQntuples(_result)) if (index > PQntuples(_result))
@throw [OFOutOfRangeException @throw [OFOutOfRangeException exception];
exceptionWithClass: [self class]];
return [PGResultRow rowWithResult: self return [PGResultRow rowWithResult: self
row: (int)index]; row: (int)index];

View file

@ -124,7 +124,7 @@ convert_type(PGresult *res, int column, OFString *string)
} }
if (count > SIZE_MAX - state->state) if (count > SIZE_MAX - state->state)
@throw [OFOutOfRangeException exceptionWithClass: [self class]]; @throw [OFOutOfRangeException exception];
if (state->state + count > state->extra[1]) if (state->state + count > state->extra[1])
count = state->extra[1] - state->state; count = state->extra[1] - state->state;

View file

@ -9,11 +9,9 @@
@property (readonly, copy, nonatomic) OFString *command; @property (readonly, copy, nonatomic) OFString *command;
#endif #endif
+ exceptionWithClass: (Class)class_ + (instancetype)exceptionWithConnection: (PGConnection*)connection
connection: (PGConnection*)connection command: (OFString*)command;
- initWithConnection: (PGConnection*)connection
command: (OFString*)command; command: (OFString*)command;
- initWithClass: (Class)class_
connection: (PGConnection*)connection
command: (OFString*)command;
- (OFString*)command; - (OFString*)command;
@end @end

View file

@ -1,21 +1,17 @@
#import "PGCommandFailedException.h" #import "PGCommandFailedException.h"
@implementation PGCommandFailedException @implementation PGCommandFailedException
+ exceptionWithClass: (Class)class + (instancetype)exceptionWithConnection: (PGConnection*)connection
connection: (PGConnection*)connection command: (OFString*)command
command: (OFString*)command
{ {
return [[[self alloc] initWithClass: class return [[[self alloc] initWithConnection: connection
connection: connection command: command] autorelease];
command: command] autorelease];
} }
- initWithClass: (Class)class - initWithConnection: (PGConnection*)connection
connection: (PGConnection*)connection command: (OFString*)command
command: (OFString*)command
{ {
self = [super initWithClass: class self = [super initWithConnection: connection];
connection: connection];
@try { @try {
_command = [command copy]; _command = [command copy];
@ -36,10 +32,8 @@
- (OFString*)description - (OFString*)description
{ {
return [OFString stringWithFormat: return [OFString stringWithFormat: @"A PostgreSQL command failed: %@\n"
@"A PostgreSQL command in class %@ failed: %s\nCommand: %@", @"Command: %@", _error, _command];
[self inClass], PQerrorMessage([_connection PG_connection]),
_command];
} }
- (OFString*)command - (OFString*)command

View file

@ -4,9 +4,7 @@
- (OFString*)description - (OFString*)description
{ {
return [OFString stringWithFormat: return [OFString stringWithFormat:
@"Establishing a PostgreSQL connection in class %@ failed:\n%s\n" @"Establishing a PostgreSQL connection failed:\n%@\n"
"Parameters: %@", [self inClass], "Parameters: %@", _error, [_connection parameters]];
PQerrorMessage([_connection PG_connection]),
[_connection parameters]];
} }
@end @end

View file

@ -5,15 +5,14 @@
@interface PGException: OFException @interface PGException: OFException
{ {
PGConnection *_connection; PGConnection *_connection;
OFString *_error;
} }
#ifdef OF_HAVE_PROPERTIES #ifdef OF_HAVE_PROPERTIES
@property (readonly, retain, nonatomic) PGConnection *connection; @property (readonly, retain, nonatomic) PGConnection *connection;
#endif #endif
+ exceptionWithClass: (Class)class_ + (instancetype)exceptionWithConnection: (PGConnection*)connection;
connection: (PGConnection*)connection; - initWithConnection: (PGConnection*)connection;
- initWithClass: (Class)class_
connection: (PGConnection*)connection;
- (PGConnection*)connection; - (PGConnection*)connection;
@end @end

View file

@ -1,19 +1,24 @@
#import "PGException.h" #import "PGException.h"
@implementation PGException @implementation PGException
+ exceptionWithClass: (Class)class + (instancetype)exceptionWithConnection: (PGConnection*)connection
connection: (PGConnection*)connection
{ {
return [[[self alloc] initWithClass: class return [[[self alloc] initWithConnection: connection] autorelease];
connection: connection] autorelease];
} }
- initWithClass: (Class)class - initWithConnection: (PGConnection*)connection
connection: (PGConnection*)connection
{ {
self = [super initWithClass: class]; self = [super init];
_connection = [connection retain]; @try {
_connection = [connection retain];
_error = [[OFString alloc]
initWithCString: PQerrorMessage([_connection PG_connection])
encoding: OF_STRING_ENCODING_NATIVE];
} @catch (id e) {
[self release];
@throw e;
}
return self; return self;
} }
@ -21,15 +26,15 @@
- (void)dealloc - (void)dealloc
{ {
[_connection release]; [_connection release];
[_error release];
[super dealloc]; [super dealloc];
} }
- (OFString*)description - (OFString*)description
{ {
return [OFString stringWithFormat: return [OFString stringWithFormat: @"A PostgreSQL operation failed: %@",
@"A PostgreSQL operation in class %@ failed: %s", [self inClass], _error];
PQerrorMessage([_connection PG_connection])];
} }
- (PGConnection*)connection - (PGConnection*)connection