2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "NGStreamExceptions.h"
26 @interface NSObject(setLastException)
27 - (void)setLastException:(NSException *)_exception;
30 @implementation NGIOException
33 self = [super initWithName:NSStringFromClass([self class])
34 reason:@"an IO exception occured"
38 - (id)initWithReason:(NSString *)_reason {
39 self = [super initWithName:NSStringFromClass([self class])
45 + (void)raiseWithReason:(NSString *)_reason {
46 [[[self alloc] initWithReason:_reason] raise];
49 + (void)raiseOnStream:(id)_stream reason:(NSString *)_reason {
52 e = [[self alloc] initWithReason:_reason];
55 if ([_stream respondsToSelector:@selector(setLastException:)]) {
56 [_stream setLastException:e];
63 + (void)raiseOnStream:(id)_stream {
64 [self raiseOnStream:_stream reason:nil];
67 @end /* NGIOException */
69 @implementation NGStreamException
71 - (NSString *)defaultReason {
72 return @"a stream exception occured";
76 return [self initWithStream:nil reason:[self defaultReason]];
78 - (id)initWithStream:(id<NGStream>)_stream {
79 return [self initWithStream:_stream reason:[self defaultReason]];
82 - (id)initWithStream:(id<NGStream>)_stream format:(NSString *)_format,... {
86 va_start(ap, _format);
87 tmp = [[NSString alloc] initWithFormat:_format arguments:ap];
90 self = [self initWithStream:_stream reason:tmp];
95 - (id)initWithStream:(id<NGStream>)_stream reason:(NSString *)_reason {
96 NSDictionary *ui = nil;
98 ui = [NSDictionary dictionaryWithObjectsAndKeys:
99 [NSNumber numberWithInt:errno], @"errno",
100 [NSString stringWithCString:strerror(errno)], @"error",
101 [NSValue valueWithNonretainedObject:_stream], @"stream",
104 self = [super initWithName:NSStringFromClass([self class])
108 self->streamPointer =
109 [[NSValue valueWithNonretainedObject:_stream] retain];
114 + (id)exceptionWithStream:(id<NGStream>)_stream {
115 return [[self alloc] initWithStream:_stream];
117 + (id)exceptionWithStream:(id<NGStream>)_stream reason:(NSString *)_reason {
118 return [[self alloc] initWithStream:_stream reason:_reason];
120 + (void)raiseWithStream:(id<NGStream>)_stream {
121 [[[self alloc] initWithStream:_stream] raise];
123 + (void)raiseWithStream:(id<NGStream>)_stream reason:(NSString *)_reason {
124 [[[self alloc] initWithStream:_stream reason:_reason] raise];
126 + (void)raiseWithStream:(id<NGStream>)_stream format:(NSString *)_format,... {
130 va_start(ap, _format);
131 tmp = [[NSString alloc] initWithFormat:_format arguments:ap];
133 tmp = [tmp autorelease];
135 [[[self alloc] initWithStream:_stream reason:tmp] raise];
139 [self->streamPointer release];
143 @end /* NGStreamException */
145 // ******************** NGEndOfStreamException ********************
147 @implementation NGEndOfStreamException
149 - (id)initWithStream:(id<NGStream>)_stream {
150 return [self initWithStream:_stream
156 - (id)initWithStream:(id<NGStream>)_stream
157 readCount:(unsigned)_readCount
158 safeCount:(unsigned)_safeCount
159 data:(NSData *)_data {
163 tmp = [NSString stringWithFormat:@"reached end of stream %@", _stream];
165 if ((self = [super initWithStream:_stream reason:tmp])) {
166 self->readCount = _readCount;
167 self->safeCount = _safeCount;
168 self->data = [_data retain];
174 [self->data release];
180 - (NSData *)readBytes {
184 @end /* NGEndOfStreamException */
186 // ******************** open state exceptions *********************
188 @implementation NGCouldNotOpenStreamException
190 - (NSString *)defaultReason {
191 return @"could not open stream";
196 @implementation NGCouldNotCloseStreamException
198 - (NSString *)defaultReason {
199 return @"could not close stream";
204 @implementation NGStreamNotOpenException
206 - (NSString *)defaultReason {
207 return @"stream is not open";
212 // ******************** NGStreamErrors ****************************
214 @implementation NGStreamErrorException
216 - (id)initWithStream:(id<NGStream>)_stream errorCode:(int)_code {
219 tmp = [NSString stringWithFormat:@"stream error occured, errno=%i error=%s",
220 _code, strerror(_code)];
221 if ((self = [self initWithStream:_stream reason:tmp])) {
228 + (void)raiseWithStream:(id<NGStream>)_stream errorCode:(int)_code {
229 [[[self alloc] initWithStream:_stream errorCode:_code] raise];
232 - (int)operationSystemErrorCode {
235 - (NSString *)operatingSystemError {
236 return [NSString stringWithCString:strerror(osErrorCode)];
239 @end /* NGStreamErrorException */
241 @implementation NGStreamReadErrorException
243 - (NSString *)defaultReason {
244 return @"read error on stream";
247 @end /* NGStreamReadErrorException */
249 @implementation NGStreamWriteErrorException
251 - (NSString *)defaultReason {
252 return @"write error on stream";
255 @end /* NGStreamWriteErrorException */
257 @implementation NGStreamSeekErrorException
259 - (NSString *)defaultReason {
260 return @"seek error on stream";
265 // ******************** NGStreamModeExceptions ********************
267 @implementation NGStreamModeException
269 - (NSString *)defaultReason {
270 return @"stream mode failure";
275 @implementation NGUnknownStreamModeException
277 - (NSString *)defaultReason {
278 return @"unknow stream mode";
281 - (id)initWithStream:(id<NGStream>)_stream mode:(NSString *)_streamMode {
282 if ((self = [super initWithStream:_stream
283 format:@"unknown stream mode: %@", _streamMode])) {
284 streamMode = [_streamMode copy];
290 [self->streamMode release];
294 @end /* NGUnknownStreamModeException */
296 @implementation NGReadOnlyStreamException
298 - (NSString *)defaultReason {
299 return @"stream is read only";
302 @end /* NGReadOnlyStreamException */
304 @implementation NGWriteOnlyStreamException
306 - (NSString *)defaultReason {
307 return @"stream is write only";
310 @end /* NGWriteOnlyStreamException */
312 // ******************** NGIOAccessException ******************
314 @implementation NGIOAccessException
317 @implementation NGIOSearchAccessException