HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
error.h
Go to the documentation of this file.
1 //
2 // Copyright 2024 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_USD_VALIDATION_USD_VALIDATION_ERROR_H
9 #define PXR_USD_VALIDATION_USD_VALIDATION_ERROR_H
10 
11 #include "pxr/pxr.h"
12 #include "pxr/usd/sdf/path.h"
13 #include "pxr/usd/usd/property.h"
14 #include "pxr/usd/usd/stage.h"
16 
17 #include <string>
18 
20 
21 class UsdPrim;
23 
24 /// \class UsdValidationErrorType
25 ///
26 /// UsdValidationErrorType reflects severity of a validation error, which can
27 /// then be reported appropriately to the users.
28 ///
29 /// None: No Error.
30 /// Error: Associates the UsdValidationErrorType with an actual Error reported
31 /// by the validation task.
32 /// Warn: Associates the UsdValidationErrorType with a less severe situation and
33 /// hence reported as warning by the validation task.
34 /// Info: Associates the UsdValidationErrorType with information which needs to
35 /// be reported to the users by the validation task.
37 {
38  None = 0,
39  Error,
40  Warn,
41  Info
42 };
43 
44 /// \class UsdValidationErrorSite
45 ///
46 /// UsdValidationErrorSite is important information available from a
47 /// ValidationError, which annotates the site where the Error was reported by a
48 /// validation task.
49 ///
50 /// An Error could be reported in a SdfLayer (in layer metadata, for example),
51 /// or a UsdStage (in stage metadata, for example) or a Prim within a stage, or
52 /// a property of a prim.
54 {
55 public:
56  UsdValidationErrorSite() = default;
57 
58  /// Initialize an UsdValidationErrorSite using a \p layer and an
59  /// \p objectPath.
60  ///
61  /// Object Path here could be a prim or a property spec path.
62  ///
63  /// Note that to identify a layer metadata, objectPath can be set as the
64  /// pseudoRoot.
66  UsdValidationErrorSite(const SdfLayerHandle &layer,
67  const SdfPath &objectPath);
68 
69  /// Initialize an UsdValidationErrorSite using a \p usdStage and an \p
70  /// objectPath.
71  ///
72  /// An option \p layer can also be provided to provide information about a
73  /// specific layer the erroring \p objectPath is found in the property
74  /// stack.
75  ///
76  /// Object Path here could be a prim path or a property path.
77  /// Note that to identify stage's root layer metadata, objectPath can be set
78  /// as the pseudoRoot.
80  UsdValidationErrorSite(const UsdStagePtr &usdStage,
81  const SdfPath &objectPath,
82  const SdfLayerHandle &layer = SdfLayerHandle());
83 
84  /// Returns true if UsdValidationErrorSite instance can either point to a
85  /// prim or property spec in a layer or a prim or property on a stage.
86  bool IsValid() const
87  {
88  return IsValidSpecInLayer() || IsPrim() || IsProperty();
89  }
90 
91  /// Returns true if the objectPath and layer represent a spec in the layer;
92  /// false otherwise.
93  bool IsValidSpecInLayer() const
94  {
95  if (!_layer || _objectPath.IsEmpty()) {
96  return false;
97  }
98  return _layer->HasSpec(_objectPath);
99  }
100 
101  /// Returns true if UsdValidationErrorSite represents a prim on a stage,
102  /// false otherwise.
103  bool IsPrim() const
104  {
105  return GetPrim().IsValid();
106  }
107 
108  /// Returns true if UsdValidationErrorSite represents a property on a stage,
109  /// false otherwise.
110  bool IsProperty() const
111  {
112  return GetProperty().IsValid();
113  }
114 
115  /// Returns the SdfPropertySpecHandle associated with this
116  /// ValidationErrorSite's layer and objectPath.
117  ///
118  /// Returns an invalid SdfPropertySpecHandle if no valid property spec is
119  /// found, or when UsdValidationErrorSite instance doesn't have a
120  /// layer.
121  const SdfPropertySpecHandle GetPropertySpec() const
122  {
123  if (!_layer) {
124  return SdfPropertySpecHandle();
125  }
126  return _layer->GetPropertyAtPath(_objectPath);
127  }
128 
129  /// Returns the SdfPrimSpecHandle associated with this ValidationErrorSite's
130  /// layer and objectPath.
131  ///
132  /// Returns an invalid SdfPrimSpecHandle if no valid prim spec is found, or
133  /// when UsdValidationErrorSite instance doesn't have a layer.
134  const SdfPrimSpecHandle GetPrimSpec() const
135  {
136  if (!_layer) {
137  return SdfPrimSpecHandle();
138  }
139  return _layer->GetPrimAtPath(_objectPath);
140  }
141 
142  /// Returns the SdfLayerHandle associated with this
143  /// UsdValidationValidatorErrorSite
144  const SdfLayerHandle &GetLayer() const
145  {
146  return _layer;
147  }
148 
149  /// Returns the UsdStage associated with this UsdValidationErrorSite;
150  /// nullptr othewrise.
151  const UsdStagePtr &GetStage() const
152  {
153  return _usdStage;
154  }
155 
156  /// Returns UsdPrim associated with this UsdValidationErrorSite, that is
157  /// when UsdStage is present and objectPath represents a prim path on this
158  /// stage; if not, an invalid prim is returned.
159  UsdPrim GetPrim() const
160  {
161  if (_usdStage) {
162  return _usdStage->GetPrimAtPath(_objectPath);
163  }
164  return UsdPrim();
165  }
166 
167  /// Returns UsdProperty associated with this UsdValidationErrorSite, that is
168  /// when UsdStage is present and objectPath represents a property path on
169  /// this stage; if not, an invalid property is returned.
171  {
172  if (_usdStage) {
173  return _usdStage->GetPropertyAtPath(_objectPath);
174  }
175  return UsdProperty();
176  }
177 
178  /// Returns true if \p other UsdValidationErrorSite has same valued members
179  /// as this UsdValidationErrorSite, false otherwise.
180  bool operator==(const UsdValidationErrorSite &other) const
181  {
182  return (_layer == other._layer) && (_usdStage == other._usdStage)
183  && (_objectPath == other._objectPath);
184  }
185 
186  /// Returns false if \p other UsdValidationErrorSite has same valued members
187  /// as this UsdValidationErrorSite, true otherwise.
188  bool operator!=(const UsdValidationErrorSite &other) const
189  {
190  return !(*this == other);
191  }
192 
193 private:
194  UsdStagePtr _usdStage;
195  SdfLayerHandle _layer;
196  SdfPath _objectPath;
197 
198 }; // UsdValidationErrorSite
199 
200 using UsdValidationErrorSites = std::vector<UsdValidationErrorSite>;
201 
202 /// \class UsdValidationError
203 ///
204 /// UsdValidationError is an entity returned by a validation task, which is
205 /// associated with a UsdValidationValidator.
206 ///
207 /// A UsdValidationError instance contains important information, like:
208 ///
209 /// - Name - A name the validator writer provided for the error. This is then
210 /// used to construct an identifier for the error.
211 ///
212 /// - UsdValidationErrorType - severity of an error,
213 ///
214 /// - UsdValidationErrorSites - on what sites validationError was reported by a
215 /// validation task,
216 ///
217 /// - Message - Message providing more information associated with the error.
218 /// Such a message is provided by the validator writer, when providing
219 /// implementation for the validation task function.
220 ///
221 /// UsdValidationError instances will be stored in the UsdValidationContext
222 /// responsible for executing a set of UsdValidationValidators.
223 ///
225 {
226 public:
227  /// A default constructed UsdValidationError signifies no error.
230 
231  /// Instantiate a ValidationError by providing its \p name, \p errorType,
232  /// \p errorSites and an \p errorMsg.
235  const UsdValidationErrorType &errorType,
236  const UsdValidationErrorSites &errorSites,
237  const std::string &errorMsg);
238 
239  bool operator==(const UsdValidationError &other) const
240  {
241  return (_name == other._name) && (_errorType == other._errorType)
242  && (_errorSites == other._errorSites)
243  && (_errorMsg == other._errorMsg)
244  && (_validator == other._validator);
245  }
246 
247  bool operator!=(const UsdValidationError &other) const
248  {
249  return !(*this == other);
250  }
251 
252  /// Returns the name token of the UsdValidationError
253  const TfToken &GetName() const &
254  {
255  return _name;
256  }
257 
258  /// Returns the name token of the UsdValidationError by-value
260  {
261  return std::move(_name);
262  }
263 
264  /// Returns the UsdValidationErrorType associated with this
265  /// UsdValidationError
267  {
268  return _errorType;
269  }
270 
271  /// Returns the UsdValidationErrorSite associated with this
272  /// UsdValidationError
274  {
275  return _errorSites;
276  }
277 
278  /// Returns the UsdValidationErrorSite associated with this
279  /// UsdValidationError by-value
281  {
282  return std::move(_errorSites);
283  }
284 
285  /// Returns the UsdValidationValidator that reported this error.
286  ///
287  /// This will return nullptr if there is no UsdValidationValidator
288  /// associated with this error. This will never be nullptr for validation
289  /// errors returned from calls to UsdValidationValidator::Validate.
291  {
292  return _validator;
293  }
294 
295  /// Returns the message associated with this UsdValidationError
296  const std::string &GetMessage() const
297  {
298  return _errorMsg;
299  }
300 
301  /// An identifier for the error constructed from the validator name this
302  /// error was generated from and its name.
303  ///
304  /// Since a validator may result in multiple distinct errors, the identifier
305  /// helps in distinguishing and categorizing the errors. The identifier
306  /// returned will be in the following form:
307  /// For a plugin validator: "plugName":"validatorName"."ErrorName"
308  /// For a non-plugin validator: "validatorName"."ErrorName"
309  ///
310  /// For an error that was generated without a name, the identifier will be
311  /// same as the validator name which generated the error.
312  ///
313  /// For an error which is created directly and not via
314  /// UsdValidationValidator::Validate() call, we throw a coding error, as its
315  /// an improper use of the API.
317  TfToken GetIdentifier() const;
318 
319  /// Returns UsdValidationErrorType and ErrorMessage concatenated as a string
321  std::string GetErrorAsString() const;
322 
323  /// Returns true if UsdValidationErrorType is UsdValidationErrorType::None,
324  /// false otherwise
325  bool HasNoError() const
326  {
327  return _errorType == UsdValidationErrorType::None;
328  }
329 
330 private:
331  // UsdValidationValidatorError holds a pointer to the UsdValidationValidator
332  // that generated it, so we need to provide friend access to allow the
333  // necessary mutation.
335 
336  // Used by UsdValidationValidator::Validate methods to embed itself to the
337  // reported errors.
338  void _SetValidator(const UsdValidationValidator *validator);
339 
340  // _validator is set when ValidationError is generated via a
341  // UsdValidationValidator::Validate() call.
342  const UsdValidationValidator *_validator;
343 
344  // These data members should not be modified other than during
345  // initialization by the validate task functions.
346  TfToken _name;
347  UsdValidationErrorType _errorType;
348  UsdValidationErrorSites _errorSites;
349  std::string _errorMsg;
350 
351  // TODO:(Subsequent iterations)
352  // - VtValue of a random value the error wants to propagate down to the
353  // fixer
354 
355 }; // UsdValidationError
356 
358 
359 #endif // PXR_USD_VALIDATION_USD_VALIDATION_ERROR_H
TfToken GetName()&&
Returns the name token of the UsdValidationError by-value.
Definition: error.h:259
bool HasNoError() const
Definition: error.h:325
USDVALIDATION_API TfToken GetIdentifier() const
UsdProperty GetProperty() const
Definition: error.h:170
UsdPrim GetPrim() const
Definition: error.h:159
bool IsValidSpecInLayer() const
Definition: error.h:93
const UsdStagePtr & GetStage() const
Definition: error.h:151
bool operator==(const UsdValidationError &other) const
Definition: error.h:239
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:398
bool IsProperty() const
Definition: error.h:110
UsdValidationErrorSites GetSites()&&
Definition: error.h:280
const SdfPrimSpecHandle GetPrimSpec() const
Definition: error.h:134
const TfToken & GetName() const &
Returns the name token of the UsdValidationError.
Definition: error.h:253
USDVALIDATION_API UsdValidationError()
A default constructed UsdValidationError signifies no error.
const std::string & GetMessage() const
Returns the message associated with this UsdValidationError.
Definition: error.h:296
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
#define USDVALIDATION_API
Definition: api.h:25
bool operator==(const UsdValidationErrorSite &other) const
Definition: error.h:180
Definition: token.h:70
bool operator!=(const UsdValidationErrorSite &other) const
Definition: error.h:188
const SdfLayerHandle & GetLayer() const
Definition: error.h:144
const UsdValidationErrorSites & GetSites() const &
Definition: error.h:273
USDVALIDATION_API std::string GetErrorAsString() const
Returns UsdValidationErrorType and ErrorMessage concatenated as a string.
Definition: prim.h:116
bool IsValid() const
Definition: error.h:86
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:273
std::vector< UsdValidationErrorSite > UsdValidationErrorSites
Definition: error.h:200
bool IsPrim() const
Definition: error.h:103
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
bool IsValid() const
Return true if this is a valid object, false otherwise.
Definition: object.h:126
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
const UsdValidationValidator * GetValidator() const
Definition: error.h:290
const SdfPropertySpecHandle GetPropertySpec() const
Definition: error.h:121
UsdValidationErrorType GetType() const
Definition: error.h:266
UsdValidationErrorSite()=default
bool operator!=(const UsdValidationError &other) const
Definition: error.h:247