2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "NGStreamExceptions.h"
25 @interface NSObject(setLastException)
26 - (void)setLastException:(NSException *)_exception;
29 @implementation NGIOException
32 self = [super initWithName:NSStringFromClass([self class])
33 reason:@"an IO exception occured"
37 - (id)initWithReason:(NSString *)_reason {
38 self = [super initWithName:NSStringFromClass([self class])
44 + (void)raiseWithReason:(NSString *)_reason {
45 [[[self alloc] initWithReason:_reason] raise];
48 + (void)raiseOnStream:(id)_stream reason:(NSString *)_reason {
51 e = [[self alloc] initWithReason:_reason];
54 if ([_stream respondsToSelector:@selector(setLastException:)]) {
55 [_stream setLastException:e];
62 + (void)raiseOnStream:(id)_stream {
63 [self raiseOnStream:_stream reason:nil];
66 @end /* NGIOException */
68 @implementation NGStreamException
70 - (NSString *)defaultReason {
71 return @"a stream exception occured";
75 return [self initWithStream:nil reason:[self defaultReason]];
77 - (id)initWithStream:(id<NGStream>)_stream {
78 return [self initWithStream:_stream reason:[self defaultReason]];
81 - (id)initWithStream:(id<NGStream>)_stream format:(NSString *)_format,... {
85 va_start(ap, _format);
86 tmp = [[NSString alloc] initWithFormat:_format arguments:ap];
89 self = [self initWithStream:_stream reason:tmp];
94 - (id)initWithStream:(id<NGStream>)_stream reason:(NSString *)_reason {
95 NSDictionary *ui = nil;
97 ui = [NSDictionary dictionaryWithObjectsAndKeys:
98 [NSNumber numberWithInt:errno], @"errno",
99 [NSString stringWithCString:strerror(errno)], @"error",
100 [NSValue valueWithNonretainedObject:_stream], @"stream",
103 self = [super initWithName:NSStringFromClass([self class])
107 self->streamPointer =
108 [[NSValue valueWithNonretainedObject:_stream] retain];
113 + (id)exceptionWithStream:(id<NGStream>)_stream {
114 return [[self alloc] initWithStream:_stream];
116 + (id)exceptionWithStream:(id<NGStream>)_stream reason:(NSString *)_reason {
117 return [[self alloc] initWithStream:_stream reason:_reason];
119 + (void)raiseWithStream:(id<NGStream>)_stream {
120 [[[self alloc] initWithStream:_stream] raise];
122 + (void)raiseWithStream:(id<NGStream>)_stream reason:(NSString *)_reason {
123 [[[self alloc] initWithStream:_stream reason:_reason] raise];
125 + (void)raiseWithStream:(id<NGStream>)_stream format:(NSString *)_format,... {
129 va_start(ap, _format);
130 tmp = [[NSString alloc] initWithFormat:_format arguments:ap];
132 tmp = [tmp autorelease];
134 [[[self alloc] initWithStream:_stream reason:tmp] raise];
138 [self->streamPointer release];
142 @end /* NGStreamException */
144 // ******************** NGEndOfStreamException ********************
146 @implementation NGEndOfStreamException
148 - (id)initWithStream:(id<NGStream>)_stream {
149 return [self initWithStream:_stream
155 - (id)initWithStream:(id<NGStream>)_stream
156 readCount:(unsigned)_readCount
157 safeCount:(unsigned)_safeCount
158 data:(NSData *)_data {
162 tmp = [NSString stringWithFormat:@"reached end of stream %@", _stream];
164 if ((self = [super initWithStream:_stream reason:tmp])) {
165 self->readCount = _readCount;
166 self->safeCount = _safeCount;
167 self->data = [_data retain];
173 [self->data release];
179 - (NSData *)readBytes {
183 @end /* NGEndOfStreamException */
185 // ******************** open state exceptions *********************
187 @implementation NGCouldNotOpenStreamException
189 - (NSString *)defaultReason {
190 return @"could not open stream";
195 @implementation NGCouldNotCloseStreamException
197 - (NSString *)defaultReason {
198 return @"could not close stream";
203 @implementation NGStreamNotOpenException
205 - (NSString *)defaultReason {
206 return @"stream is not open";
211 // ******************** NGStreamErrors ****************************
213 @implementation NGStreamErrorException
215 - (id)initWithStream:(id<NGStream>)_stream errorCode:(int)_code {
218 tmp = [NSString stringWithFormat:@"stream error occured, errno=%i error=%s",
219 _code, strerror(_code)];
220 if ((self = [self initWithStream:_stream reason:tmp])) {
227 + (void)raiseWithStream:(id<NGStream>)_stream errorCode:(int)_code {
228 [[[self alloc] initWithStream:_stream errorCode:_code] raise];
231 - (int)operationSystemErrorCode {
234 - (NSString *)operatingSystemError {
235 return [NSString stringWithCString:strerror(osErrorCode)];
238 @end /* NGStreamErrorException */
240 @implementation NGStreamReadErrorException
242 - (NSString *)defaultReason {
243 return @"read error on stream";
246 @end /* NGStreamReadErrorException */
248 @implementation NGStreamWriteErrorException
250 - (NSString *)defaultReason {
251 return @"write error on stream";
254 @end /* NGStreamWriteErrorException */
256 @implementation NGStreamSeekErrorException
258 - (NSString *)defaultReason {
259 return @"seek error on stream";
264 // ******************** NGStreamModeExceptions ********************
266 @implementation NGStreamModeException
268 - (NSString *)defaultReason {
269 return @"stream mode failure";
274 @implementation NGUnknownStreamModeException
276 - (NSString *)defaultReason {
277 return @"unknow stream mode";
280 - (id)initWithStream:(id<NGStream>)_stream mode:(NSString *)_streamMode {
281 if ((self = [super initWithStream:_stream
282 format:@"unknown stream mode: %@", _streamMode])) {
283 streamMode = [_streamMode copy];
289 [self->streamMode release];
293 @end /* NGUnknownStreamModeException */
295 @implementation NGReadOnlyStreamException
297 - (NSString *)defaultReason {
298 return @"stream is read only";
301 @end /* NGReadOnlyStreamException */
303 @implementation NGWriteOnlyStreamException
305 - (NSString *)defaultReason {
306 return @"stream is write only";
309 @end /* NGWriteOnlyStreamException */
311 // ******************** NGIOAccessException ******************
313 @implementation NGIOAccessException
316 @implementation NGIOSearchAccessException