~ubuntu-branches/ubuntu/gutsy/soprano/gutsy

« back to all changes in this revision

Viewing changes to soprano/error.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-10-12 14:43:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071012144348-yajzi51v4k23ahxf
Tags: 1.95.0~beta2-1ubuntu1
* Sync with Debian
* Add versioned build-dep on raptor

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 */
21
21
 
22
22
#include "error.h"
23
 
 
24
 
static char* s_errorMessages[] = {
25
 
  "Success",
26
 
  "Invalid statement",
27
 
  "Unknown error"
 
23
#include "locator.h"
 
24
 
 
25
#include <QtCore/QHash>
 
26
#include <QtCore/QThread>
 
27
#include <QtCore/QDebug>
 
28
 
 
29
 
 
30
namespace Soprano {
 
31
    namespace Error {
 
32
        class ErrorData : public QSharedData
 
33
        {
 
34
        public:
 
35
            ErrorData( const QString& m = QString(), int c = ErrorNone )
 
36
                : message( m ),
 
37
                  code( c ) {
 
38
            }
 
39
 
 
40
            virtual ~ErrorData() {
 
41
            }
 
42
 
 
43
            QString message;
 
44
            int code;
 
45
        };
 
46
    }
 
47
}
 
48
 
 
49
 
 
50
Soprano::Error::Error::Error()
 
51
    : d( new ErrorData() )
 
52
{
 
53
}
 
54
 
 
55
 
 
56
Soprano::Error::Error::Error( ErrorData* data )
 
57
    : d( data )
 
58
{
 
59
}
 
60
 
 
61
 
 
62
Soprano::Error::Error::Error( const QString& message, int code )
 
63
    : d( new ErrorData( message,  code ) )
 
64
{
 
65
    if ( d->message.isEmpty() && code < ErrorUnknown ) {
 
66
        d->message = errorMessage( ( ErrorCode )code );
 
67
    }
 
68
}
 
69
 
 
70
 
 
71
Soprano::Error::Error::Error( const Error& other )
 
72
{
 
73
    d = other.d;
 
74
}
 
75
 
 
76
 
 
77
Soprano::Error::Error::~Error()
 
78
{
 
79
}
 
80
 
 
81
 
 
82
Soprano::Error::Error& Soprano::Error::Error::operator=( const Error& other )
 
83
{
 
84
    d = other.d;
 
85
    return *this;
 
86
}
 
87
 
 
88
 
 
89
QString Soprano::Error::Error::message() const
 
90
{
 
91
    return d->message;
 
92
}
 
93
 
 
94
 
 
95
int Soprano::Error::Error::code() const
 
96
{
 
97
    return d->code;
 
98
}
 
99
 
 
100
 
 
101
namespace Soprano {
 
102
    namespace Error {
 
103
        class ParserErrorData : public Soprano::Error::ErrorData
 
104
        {
 
105
        public:
 
106
            ParserErrorData( const Locator& loc = Locator(), const QString& message = QString(), int code = ErrorNone )
 
107
                : ErrorData( message, code ),
 
108
                  locator( loc ) {
 
109
            }
 
110
 
 
111
            Locator locator;
 
112
        };
 
113
    }
 
114
}
 
115
 
 
116
bool Soprano::Error::Error::isParserError() const
 
117
{
 
118
    return dynamic_cast<const ParserErrorData*>( d.constData() ) != 0;
 
119
}
 
120
 
 
121
 
 
122
Soprano::Error::ParserError Soprano::Error::Error::toParserError() const
 
123
{
 
124
    return ParserError( *this );
 
125
}
 
126
 
 
127
 
 
128
Soprano::Error::ParserError::ParserError()
 
129
    : Error( new ParserErrorData() )
 
130
{
 
131
}
 
132
 
 
133
 
 
134
Soprano::Error::ParserError::ParserError( const Locator& loc, const QString& message, int code )
 
135
    : Error( new ParserErrorData( loc, message, code ) )
 
136
{
 
137
}
 
138
 
 
139
 
 
140
Soprano::Error::ParserError::ParserError( const Error& other )
 
141
    : Error( other )
 
142
{
 
143
}
 
144
 
 
145
 
 
146
Soprano::Error::ParserError::~ParserError()
 
147
{
 
148
}
 
149
 
 
150
 
 
151
Soprano::Error::ParserError& Soprano::Error::ParserError::operator=( const Error& other )
 
152
{
 
153
    Error::operator=( other );
 
154
    return *this;
 
155
}
 
156
 
 
157
 
 
158
Soprano::Error::Locator Soprano::Error::ParserError::locator() const
 
159
{
 
160
    const ParserErrorData* data = dynamic_cast<const ParserErrorData*>( d.constData() );
 
161
    if ( data ) {
 
162
        return data->locator;
 
163
    }
 
164
    else {
 
165
        return Locator();
 
166
    }
 
167
}
 
168
 
 
169
 
 
170
 
 
171
// /////////////////////////////////////
 
172
// ERROR CACHE
 
173
// /////////////////////////////////////
 
174
 
 
175
 
 
176
class Soprano::Error::ErrorCache::Private
 
177
{
 
178
public:
 
179
    QHash<QThread*, Error> errorMap;
28
180
};
29
181
 
30
 
QString Soprano::errorMessage( ErrorCode code )
31
 
{
32
 
  // FIXME: translate the strings.
33
 
  return s_errorMessages[(int)code];
 
182
 
 
183
Soprano::Error::ErrorCache::ErrorCache()
 
184
    : d( new Private() )
 
185
{
 
186
}
 
187
 
 
188
 
 
189
Soprano::Error::ErrorCache::~ErrorCache()
 
190
{
 
191
    delete d;
 
192
}
 
193
 
 
194
 
 
195
Soprano::Error::Error Soprano::Error::ErrorCache::lastError() const
 
196
{
 
197
    return d->errorMap[QThread::currentThread()];
 
198
}
 
199
 
 
200
 
 
201
void Soprano::Error::ErrorCache::setError( const Error& error ) const
 
202
{
 
203
    if ( error ) {
 
204
        qDebug() << "(Soprano) Error occured in thread" << QThread::currentThreadId() << ":" << error;
 
205
    }
 
206
 
 
207
    d->errorMap[QThread::currentThread()] = error;
 
208
}
 
209
 
 
210
 
 
211
void Soprano::Error::ErrorCache::setError( const QString& errorMessage, int code ) const
 
212
{
 
213
    setError( Error( errorMessage, code ) );
 
214
}
 
215
 
 
216
 
 
217
void Soprano::Error::ErrorCache::clearError() const
 
218
{
 
219
    d->errorMap[QThread::currentThread()] = Error();
 
220
}
 
221
 
 
222
 
 
223
namespace {
 
224
    const int s_maxErr = 3;
 
225
    const char* s_errorMessages[] = {
 
226
        "Success",
 
227
        "Invalid argument",
 
228
        "Unsupported operation",
 
229
        "Parsing failed",
 
230
        0
 
231
    };
 
232
}
 
233
 
 
234
QString Soprano::Error::errorMessage( ErrorCode code )
 
235
{
 
236
    // FIXME: translate the strings.
 
237
    if ( code >= 0 && code <= s_maxErr ) {
 
238
        return s_errorMessages[(int)code];
 
239
    }
 
240
    else {
 
241
        return "Unknown error";
 
242
    }
 
243
}
 
244
 
 
245
 
 
246
Soprano::Error::ErrorCode Soprano::Error::convertErrorCode( int code )
 
247
{
 
248
    if ( code >= 0 && code <= s_maxErr ) {
 
249
        return ( ErrorCode )code;
 
250
    }
 
251
    else {
 
252
        return ErrorUnknown;
 
253
    }
 
254
}
 
255
 
 
256
 
 
257
QDebug operator<<( QDebug s, const Soprano::Error::Error& error )
 
258
{
 
259
    if ( error.code() < Soprano::Error::ErrorUnknown ) {
 
260
        s.nospace() << errorMessage( ( Soprano::Error::ErrorCode )error.code() ) << ": ";
 
261
    }
 
262
    s.nospace() << error.message();
 
263
 
 
264
    if ( error.isParserError() ) {
 
265
        Soprano::Error::ParserError pe( error );
 
266
        s.nospace() << " (line: " << pe.locator().line() << ", column: " << pe.locator().column() << ")";
 
267
    }
 
268
 
 
269
    return s;
 
270
}
 
271
 
 
272
 
 
273
QTextStream& operator<<( QTextStream& s, const Soprano::Error::Error& error )
 
274
{
 
275
    if ( error.code() < Soprano::Error::ErrorUnknown ) {
 
276
        s << errorMessage( ( Soprano::Error::ErrorCode )error.code() ) << ": ";
 
277
    }
 
278
    s << error.message();
 
279
 
 
280
    if ( error.isParserError() ) {
 
281
        Soprano::Error::ParserError pe( error );
 
282
        s << " (line: " << pe.locator().line() << ", column: " << pe.locator().column() << ")";
 
283
    }
 
284
 
 
285
    return s;
34
286
}