Skip to main content

json_ld/core/error/
mod.rs

1use rdf_syntax::Id;
2
3use crate::{JsonLdBacktraceBuilder, JsonLdLenient, JsonLdLoadError, JsonLdLocated};
4
5mod code;
6
7pub use code::*;
8
9pub type JsonLdLocatedError = Box<JsonLdLocated<JsonLdError>>;
10
11#[derive(Debug, thiserror::Error)]
12pub enum JsonLdError {
13	#[error("Invalid context nullification")]
14	InvalidContextNullification,
15
16	#[error("Remote document loading failed")]
17	LoadingDocumentFailed,
18
19	#[error("Processing mode conflict")]
20	ProcessingModeConflict,
21
22	#[error("Invalid context: {0}")]
23	ContextSyntax(String),
24
25	#[error("Invalid `@context` entry")]
26	InvalidContextEntry,
27
28	#[error("Invalid `@import` value")]
29	InvalidImportValue,
30
31	#[error("Invalid remote context")]
32	InvalidRemoteContext,
33
34	#[error("Invalid base IRI")]
35	InvalidBaseIri,
36
37	#[error("Invalid base direction")]
38	InvalidBaseDirection,
39
40	#[error("Invalid vocabulary mapping")]
41	InvalidVocabMapping,
42
43	#[error("Cyclic IRI mapping")]
44	CyclicIriMapping,
45
46	#[error("Invalid term definition")]
47	InvalidTermDefinition,
48
49	#[error("JsonLdKeyword redefinition")]
50	KeywordRedefinition,
51
52	#[error("Invalid `@protected` value")]
53	InvalidProtectedValue,
54
55	#[error("Invalid type mapping")]
56	InvalidTypeMapping,
57
58	#[error("Invalid reverse property")]
59	InvalidReverseProperty,
60
61	#[error("Invalid IRI mapping")]
62	InvalidIriMapping,
63
64	#[error("Invalid keyword alias")]
65	InvalidKeywordAlias,
66
67	#[error("Invalid container mapping")]
68	InvalidContainerMapping,
69
70	#[error("Invalid scoped context")]
71	InvalidScopedContext,
72
73	#[error("Protected term redefinition")]
74	ProtectedTermRedefinition,
75
76	#[error(transparent)]
77	ContextLoadingFailed(#[from] JsonLdLoadError),
78
79	#[error("Unable to parse JSON-LD context: {0}")]
80	RemoteContextSyntax(String),
81
82	#[error("Invalid `@index` value")]
83	InvalidIndexValue,
84
85	#[error("Invalid typed value")]
86	InvalidTypedValue,
87
88	#[error("Invalid value object")]
89	InvalidValueObject,
90
91	#[error("Invalid value object value")]
92	InvalidValueObjectValue,
93
94	#[error("Invalid set or list object")]
95	InvalidSetOrListObject,
96
97	#[error("Invalid `@reverse` property map")]
98	InvalidReversePropertyMap,
99
100	#[error("Invalid `@type` value")]
101	InvalidTypeValue,
102
103	#[error("Key `{0}` expansion failed")]
104	KeyExpansionFailed(String),
105
106	#[error("Invalid `@reverse` property value")]
107	InvalidReversePropertyValue,
108
109	#[error("Invalid language-tagged string")]
110	InvalidLanguageTaggedString,
111
112	#[error("Invalid language-tagged value")]
113	InvalidLanguageTaggedValue,
114
115	#[error("Invalid `@language` map value")]
116	InvalidLanguageMapValue,
117
118	#[error("Colliding keywords")]
119	CollidingKeywords,
120
121	#[error(transparent)]
122	ConflictingIndexes(#[from] ConflictingIndexes),
123
124	#[error("Invalid `@id` value")]
125	InvalidIdValue,
126
127	#[error("Invalid `@included` value")]
128	InvalidIncludedValue,
129
130	#[error("Invalid `@reverse` value")]
131	InvalidReverseValue,
132
133	#[error("Invalid `@nest` value")]
134	InvalidNestValue,
135
136	#[error("Duplicate key `{0}`")]
137	DuplicateKey(json_syntax::object::Key),
138
139	#[error("IRI confused with prefix")]
140	IriConfusedWithPrefix,
141}
142
143impl JsonLdError {
144	pub fn at(self, location: JsonLdBacktraceBuilder<'_>) -> JsonLdLocatedError {
145		Box::new(JsonLdLocated::new(self, location.build()))
146	}
147
148	pub fn duplicate_key_ref(d: json_syntax::object::DuplicateEntryRef) -> Self {
149		Self::DuplicateKey(d.0.0.clone())
150	}
151
152	pub fn code(&self) -> JsonLdErrorCode {
153		match self {
154			Self::InvalidContextNullification => JsonLdErrorCode::InvalidContextNullification,
155			Self::LoadingDocumentFailed => JsonLdErrorCode::LoadingDocumentFailed,
156			Self::ProcessingModeConflict => JsonLdErrorCode::ProcessingModeConflict,
157			Self::ContextSyntax(_) => JsonLdErrorCode::InvalidContextEntry,
158			Self::InvalidContextEntry => JsonLdErrorCode::InvalidContextEntry,
159			Self::InvalidImportValue => JsonLdErrorCode::InvalidImportValue,
160			Self::InvalidRemoteContext => JsonLdErrorCode::InvalidRemoteContext,
161			Self::InvalidBaseIri => JsonLdErrorCode::InvalidBaseIri,
162			Self::InvalidBaseDirection => JsonLdErrorCode::InvalidBaseDirection,
163			Self::InvalidVocabMapping => JsonLdErrorCode::InvalidVocabMapping,
164			Self::CyclicIriMapping => JsonLdErrorCode::CyclicIriMapping,
165			Self::InvalidTermDefinition => JsonLdErrorCode::InvalidTermDefinition,
166			Self::KeywordRedefinition => JsonLdErrorCode::KeywordRedefinition,
167			Self::InvalidProtectedValue => JsonLdErrorCode::InvalidPropagateValue,
168			Self::InvalidTypeMapping => JsonLdErrorCode::InvalidTypeMapping,
169			Self::InvalidReverseProperty => JsonLdErrorCode::InvalidReverseProperty,
170			Self::InvalidIriMapping => JsonLdErrorCode::InvalidIriMapping,
171			Self::InvalidKeywordAlias => JsonLdErrorCode::InvalidKeywordAlias,
172			Self::InvalidContainerMapping => JsonLdErrorCode::InvalidContainerMapping,
173			Self::InvalidScopedContext => JsonLdErrorCode::InvalidScopedContext,
174			Self::ProtectedTermRedefinition => JsonLdErrorCode::ProtectedTermRedefinition,
175			Self::ContextLoadingFailed(_) => JsonLdErrorCode::LoadingRemoteContextFailed,
176			Self::RemoteContextSyntax(_) => JsonLdErrorCode::LoadingRemoteContextFailed,
177			Self::InvalidIndexValue => JsonLdErrorCode::InvalidIndexValue,
178			Self::InvalidTypedValue => JsonLdErrorCode::InvalidTypedValue,
179			Self::InvalidValueObject => JsonLdErrorCode::InvalidValueObject,
180			Self::InvalidValueObjectValue => JsonLdErrorCode::InvalidValueObjectValue,
181			Self::InvalidSetOrListObject => JsonLdErrorCode::InvalidSetOrListObject,
182			Self::InvalidReversePropertyMap => JsonLdErrorCode::InvalidReversePropertyMap,
183			Self::InvalidTypeValue => JsonLdErrorCode::InvalidTypeValue,
184			Self::KeyExpansionFailed(_) => JsonLdErrorCode::KeyExpansionFailed,
185			Self::InvalidReversePropertyValue => JsonLdErrorCode::InvalidReversePropertyValue,
186			Self::InvalidLanguageTaggedString => JsonLdErrorCode::InvalidLanguageTaggedString,
187			Self::InvalidLanguageTaggedValue => JsonLdErrorCode::InvalidLanguageTaggedValue,
188			Self::InvalidLanguageMapValue => JsonLdErrorCode::InvalidLanguageMapValue,
189			Self::CollidingKeywords => JsonLdErrorCode::CollidingKeywords,
190			Self::ConflictingIndexes(_) => JsonLdErrorCode::ConflictingIndexes,
191			Self::InvalidIdValue => JsonLdErrorCode::InvalidIdValue,
192			Self::InvalidIncludedValue => JsonLdErrorCode::InvalidIncludedValue,
193			Self::InvalidReverseValue => JsonLdErrorCode::InvalidReverseValue,
194			Self::InvalidNestValue => JsonLdErrorCode::InvalidNestValue,
195			Self::DuplicateKey(_) => JsonLdErrorCode::DuplicateKey,
196			Self::IriConfusedWithPrefix => JsonLdErrorCode::IriConfusedWithPrefix,
197		}
198	}
199}
200
201/// Conflicting indexes error.
202///
203/// Raised when a single node is declared with two different indexes.
204#[derive(Clone, Debug, thiserror::Error)]
205#[error("Index `{defined_index}` conflicts with index `{conflicting_index}`")]
206pub struct ConflictingIndexes {
207	pub node_id: JsonLdLenient<Id>,
208	/// The previously-declared index, with its source location if known.
209	pub defined_index: JsonLdLocated<String>,
210	pub conflicting_index: String,
211}