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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#![deny(unused_crate_dependencies)]

use thiserror::Error;

#[allow(missing_docs)]
#[derive(Error, Debug)]
pub enum Error {
    #[allow(dead_code)]
    #[error("Unknown Error")]
    Unknown,

    #[allow(dead_code)]
    #[error("Not Implemented")]
    NotImplemented,

    #[error("Cannot start a new transaction")]
    CannotStartNewTransaction,

    #[allow(dead_code)]
    #[error("Invalid Input")]
    InvalidInput,

    #[allow(dead_code)]
    #[error("No Input Records")]
    NoInputRecords,

    #[allow(dead_code)]
    #[error("Timeout")]
    Timeout,

    #[allow(dead_code)]
    #[error("Mandatory Environment Variable {0} is empty")]
    EnvironmentVariableEmpty(String),

    #[allow(dead_code)]
    #[error("Mandatory Environment Variable {0} missing")]
    MandatoryEnvironmentVariableMissing(String),

    #[allow(dead_code)]
    #[error("Mandatory Environment Variable {0} is not a valid IRI")]
    MandatoryEnvironmentVariableIsNotIRI(String),

    #[allow(dead_code)]
    #[error("Service error {0}")]
    ServiceError(String),

    #[allow(dead_code)]
    #[error("No event")]
    NoEvent,

    #[allow(dead_code)]
    #[error("No subject")]
    NoSubject,

    #[allow(dead_code)]
    #[error("No predicate")]
    NoPredicate,

    #[allow(dead_code)]
    #[error(
        "Detected an unknown story input parameter [{param}] for story [{story_key}], expected \
         parameters are: {expected_params:?}"
    )]
    DetectedUnknownStoryInputParameter {
        story_key:       String,
        param:           String,
        expected_params: Vec<String>,
    },

    #[error("No base IRI specified")]
    NoBaseIRISpecified,

    #[error("No identifier namespace specified")]
    NoIdentifierNamespaceSpecified,

    #[error("Incorrect base IRI: {iri}")]
    IncorrectBaseIRI { iri: String },

    #[allow(dead_code)]
    #[error("Parse Error")]
    Parse,

    #[cfg(feature = "uuid")]
    #[error(transparent)]
    UuidParseError(#[from] uuid::Error),

    #[error("Could not fetch context and objects")]
    MissingContext,

    #[error("Could not generate metadata")]
    CouldNotGenerateMetadata,

    #[error("Could not find root project")]
    CouldNotFindRootProject,

    #[error("Could not rewrite IRI [{iri}]")]
    CouldNotRewriteIRI { iri: String },

    #[allow(dead_code)]
    #[error("Invalid Docker Image ID")]
    InvalidDockerImageId,

    #[error("Could not lock a resource: {msg}")]
    CouldNotLock { msg: String },

    #[error("Missing Identifier Base IRI")]
    MissingIdentifierBaseIRI,

    #[error("Could not create the story service client")]
    CouldNotCreateClient,

    #[error("Could not connect to the database server")]
    CouldNotConnectToServer,

    #[error("There's no context")]
    NoContextProvided,

    #[error("Invalid Story Service IRI")]
    InvalidClientIri,

    #[allow(dead_code)]
    #[error("Persona {persona_key} does not exist")]
    PersonaDoesNotExist { persona_key: String },

    #[allow(dead_code)]
    #[error("Story {use_case_key}/{story_key} does not exist")]
    StoryDoesNotExist { story_key: String, use_case_key: String },

    #[allow(dead_code)]
    #[error("UseCase {use_case_key} does not exist")]
    UseCaseDoesNotExist { use_case_key: String },

    #[cfg(feature = "serde")]
    #[error("JSON Parsing Error")]
    JSONParseError(serde_path_to_error::Error<serde_json::Error>),

    #[cfg(feature = "serde")]
    #[error(transparent)]
    SerdeJsonError(#[from] serde_json::Error),

    #[error("Invalid Story IRI")]
    InvalidStoryIri,

    #[error("Could not get story results")]
    CouldNotGetStoryResults,

    /// Represents all other cases of `std::io::Error`.
    #[error(transparent)]
    IOError(#[from] std::io::Error),

    #[error(transparent)]
    ParseIntError(#[from] std::num::ParseIntError),

    /// Encountered a syntax error in a SPARQL statement
    #[cfg(not(target_arch = "wasm32"))]
    #[error("Encountered SPARQL error \"{source:}\" in\n{statement:}")]
    SPARQLStatementError {
        #[source]
        source:    spargebra::ParseError,
        statement: String,
    },

    #[error(transparent)]
    FormatError(#[from] core::fmt::Error),

    /// Represents all other cases of `ignore::Error`
    /// (see <https://docs.rs/ignore/latest/ignore/enum.Error.html>)
    #[cfg(feature = "fs")]
    #[error(transparent)]
    WalkError(#[from] ignore::Error),

    #[cfg(feature = "iref")]
    #[error(transparent)]
    IriErrorString(#[from] iref::IriError<String>),

    #[cfg(feature = "iri-string")]
    #[error(transparent)]
    IriStringParseError(#[from] iri_string::validate::Error),

    #[error(transparent)]
    CApiError(#[from] std::ffi::NulError),

    #[cfg(all(feature = "rdftk-support", not(target_arch = "wasm32")))]
    #[error(transparent)]
    RDFTkError(#[from] rdftk_core::error::Error),

    #[cfg(all(feature = "rdftk-support", not(target_arch = "wasm32")))]
    #[error(transparent)]
    RDFTkIRIError(#[from] rdftk_iri::error::Error),

    #[error("Could not open database: {source:}")]
    CouldNotOpenDatabase { source: Box<Error> },

    #[cfg(all(not(target_arch = "wasm32"), feature = "cli"))]
    #[error(transparent)]
    ExcelWriterError(#[from] xlsxwriter::XlsxError),

    #[cfg(all(
        not(target_arch = "wasm32"),
        feature = "no-wasm",
        feature = "color-eyre"
    ))]
    #[error(transparent)]
    ColorEyreError(#[from] color_eyre::eyre::ErrReport),

    #[cfg(all(not(target_arch = "wasm32"), feature = "no-wasm", feature = "salvo"))]
    #[error(transparent)]
    InvalidHeaderValue(#[from] salvo::http::header::InvalidHeaderValue),

    #[cfg(all(feature = "salvo", not(target_arch = "wasm32")))]
    #[error(transparent)]
    SalvoError(#[from] salvo::Error),

    #[cfg(all(feature = "salvo", not(target_arch = "wasm32")))]
    #[error(transparent)]
    ToStrError(#[from] salvo_core::http::header::ToStrError),

    #[cfg(feature = "tokio")]
    #[error(transparent)]
    TokioJoinError(#[from] tokio::task::JoinError),

    #[cfg(feature = "tauri")]
    #[error(transparent)]
    TauriError(#[from] tauri::Error),

    #[cfg(feature = "gix")]
    #[error(transparent)]
    GitDiscoverUpwardsError2(#[from] gix_discover::upwards::Error),

    #[cfg(feature = "reqwest")]
    #[error(transparent)]
    ReqwestError(#[from] reqwest::Error),

    #[cfg(feature = "reqwest")]
    #[error(transparent)]
    UrlParseError(#[from] url::ParseError),

    #[cfg(all(feature = "reqwest", not(target_arch = "wasm32")))]
    #[error(transparent)]
    StreamBodyError(#[from] reqwest_streams::error::StreamBodyError),

    #[cfg(feature = "iri-string")]
    #[error(transparent)]
    IriStringCreationError(#[from] iri_string::types::CreationError<String>),

    #[cfg(feature = "iri")]
    #[error("Encountered IRI error \"{error:}\" in\n{iri:}")]
    IrefError { error: ekg_error::Error, iri: String },

    #[cfg(feature = "aws-lambda-runtime")]
    #[error(transparent)]
    LambdaError(#[from] lambda_runtime::Error),

    #[error(transparent)]
    HyperError(#[from] hyper::Error),

    #[error(transparent)]
    HttpError(#[from] hyper::http::Error),

    #[error(transparent)]
    InvalidUri(#[from] hyper::http::uri::InvalidUri),

    #[error("Invalid IRI: {0}")]
    InvalidIri(String),

    #[error("Invalid base IRI: {0}")]
    InvalidBaseIri(String),

    #[error(transparent)]
    InvalidUri2(#[from] fluent_uri::ParseError),

    #[error(transparent)]
    SerdeUrlEncodingError(#[from] serde_urlencoded::ser::Error),

    #[error("Unknown data type {data_type_id}")]
    UnknownDataType { data_type_id: u8 },
    #[error("Unknown value [{value}] for data type {data_type_xsd_iri:?}")]
    UnknownValueForDataType {
        data_type_xsd_iri: String,
        value:             String,
    },
    #[error("Unknown XSD data type {data_type_iri}")]
    UnknownXsdDataType { data_type_iri: String },
    #[error("Unknown literal value in N-Triples format: {value}")]
    UnknownNTriplesValue { value: String },

    #[cfg(feature = "tracing-subscriber")]
    #[error(transparent)]
    EnvFilterParseError(#[from] tracing_subscriber::filter::ParseError),

    #[cfg(feature = "tracing-subscriber")]
    #[error(transparent)]
    TracingSubscriberError(#[from] tracing::subscriber::SetGlobalDefaultError),

    #[cfg(feature = "tracing-subscriber")]
    #[error(transparent)]
    TracingSubscriberTryInitError(#[from] tracing_subscriber::util::TryInitError),

    #[cfg(feature = "tracing-subscriber")]
    #[error(transparent)]
    FromEnvError(#[from] tracing_subscriber::filter::FromEnvError),

    #[cfg(feature = "rdfox")]
    #[error(transparent)]
    R2D2Error(#[from] r2d2::Error),

    #[error("While {action}: {message}")]
    Exception { action: String, message: String },
    #[error(
        "The multiplicity ({multiplicity}) of a cursor row exceeded the maximum number of rows \
         ({maxrow}) for query:\n{query}"
    )]
    MultiplicityExceededMaximumNumberOfRows {
        maxrow:       usize,
        multiplicity: usize,
        query:        String,
    },
    #[error("Cannot get any argument indexes from the cursor of:\n{query}")]
    CannotGetAnyArgumentIndexes { query: String },
    #[error("Maximum number of rows ({maxrow}) has been exceeded for query:\n{query}")]
    ExceededMaximumNumberOfRows { maxrow: usize, query: String },
    #[error("Could not find a license key")]
    RDFoxLicenseFileNotFound,
    #[allow(dead_code)]
    #[error("Unknown resource")]
    UnknownResourceException,
    #[error("Could not create RDFox server")]
    CouldNotCreateRDFoxServer,
    #[error("Could not import RDF File")]
    CouldNotImportRDFFile,
    #[error("Invalid prefix name")]
    InvalidPrefixName,
    #[error("Invalid literal value")]
    InvalidLiteral,
    #[error("Could not parse IRI: {0:?}")]
    IriParseError(String),

    #[cfg(not(target_arch = "wasm32"))]
    #[error(transparent)]
    DateParseError(#[from] chrono::ParseError),
}

unsafe impl Send for Error {}

#[cfg(all(feature = "salvo", not(target_arch = "wasm32")))]
#[salvo::async_trait]
impl salvo::Writer for Error {
    async fn write(
        mut self,
        _req: &mut salvo::http::Request,
        _depot: &mut salvo::Depot,
        res: &mut salvo::http::Response,
    ) {
        res.status_code(salvo::http::StatusCode::INTERNAL_SERVER_ERROR);
        res.render("custom error");
    }
}

impl From<iref::InvalidIri<String>> for Error {
    fn from(value: iref::InvalidIri<String>) -> Self { Error::InvalidIri(value.to_string()) }
}

impl From<iref::InvalidIri<&str>> for Error {
    fn from(value: iref::InvalidIri<&str>) -> Self { Error::InvalidIri(value.to_string()) }
}

#[cfg(feature = "nom-support")]
impl<I: From<&'static str>> From<Error> for nom::Err<nom::error::Error<I>> {
    fn from(_: Error) -> Self {
        nom::Err::Error(nom::error::Error::new(
            "unknown datastore error".into(),
            nom::error::ErrorKind::Fail,
        ))
    }
}