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;
24 
25 /// \class UsdValidationErrorType
26 ///
27 /// UsdValidationErrorType reflects severity of a validation error, which can
28 /// then be reported appropriately to the users.
29 ///
30 /// None: No Error.
31 /// Error: Associates the UsdValidationErrorType with an actual Error reported
32 /// by the validation task.
33 /// Warn: Associates the UsdValidationErrorType with a less severe situation and
34 /// hence reported as warning by the validation task.
35 /// Info: Associates the UsdValidationErrorType with information which needs to
36 /// be reported to the users by the validation task.
38 {
39  None = 0,
40  Error,
41  Warn,
42  Info
43 };
44 
45 /// \class UsdValidationErrorSite
46 ///
47 /// UsdValidationErrorSite is important information available from a
48 /// ValidationError, which annotates the site where the Error was reported by a
49 /// validation task.
50 ///
51 /// An Error could be reported in a SdfLayer (in layer metadata, for example),
52 /// or a UsdStage (in stage metadata, for example) or a Prim within a stage, or
53 /// a property of a prim.
55 {
56 public:
57  UsdValidationErrorSite() = default;
58 
59  /// Initialize an UsdValidationErrorSite using a \p layer and an
60  /// \p objectPath.
61  ///
62  /// Object Path here could be a prim or a property spec path.
63  ///
64  /// Note that to identify a layer metadata, objectPath can be set as the
65  /// pseudoRoot.
67  UsdValidationErrorSite(const SdfLayerHandle &layer,
68  const SdfPath &objectPath);
69 
70  /// Initialize an UsdValidationErrorSite using a \p usdStage and an \p
71  /// objectPath.
72  ///
73  /// An option \p layer can also be provided to provide information about a
74  /// specific layer the erroring \p objectPath is found in the property
75  /// stack.
76  ///
77  /// Object Path here could be a prim path or a property path.
78  /// Note that to identify stage's root layer metadata, objectPath can be set
79  /// as the pseudoRoot.
81  UsdValidationErrorSite(const UsdStagePtr &usdStage,
82  const SdfPath &objectPath,
83  const SdfLayerHandle &layer = SdfLayerHandle());
84 
85  /// Returns true if UsdValidationErrorSite instance can either point to a
86  /// prim or property spec in a layer or a prim or property on a stage.
87  bool IsValid() const
88  {
89  return IsValidSpecInLayer() || IsPrim() || IsProperty();
90  }
91 
92  /// Returns true if the objectPath and layer represent a spec in the layer;
93  /// false otherwise.
94  bool IsValidSpecInLayer() const
95  {
96  if (!_layer || _objectPath.IsEmpty()) {
97  return false;
98  }
99  return _layer->HasSpec(_objectPath);
100  }
101 
102  /// Returns true if UsdValidationErrorSite represents a prim on a stage,
103  /// false otherwise.
104  bool IsPrim() const
105  {
106  return GetPrim().IsValid();
107  }
108 
109  /// Returns true if UsdValidationErrorSite represents a property on a stage,
110  /// false otherwise.
111  bool IsProperty() const
112  {
113  return GetProperty().IsValid();
114  }
115 
116  /// Returns the SdfPropertySpecHandle associated with this
117  /// ValidationErrorSite's layer and objectPath.
118  ///
119  /// Returns an invalid SdfPropertySpecHandle if no valid property spec is
120  /// found, or when UsdValidationErrorSite instance doesn't have a
121  /// layer.
122  const SdfPropertySpecHandle GetPropertySpec() const
123  {
124  if (!_layer) {
125  return SdfPropertySpecHandle();
126  }
127  return _layer->GetPropertyAtPath(_objectPath);
128  }
129 
130  /// Returns the SdfPrimSpecHandle associated with this ValidationErrorSite's
131  /// layer and objectPath.
132  ///
133  /// Returns an invalid SdfPrimSpecHandle if no valid prim spec is found, or
134  /// when UsdValidationErrorSite instance doesn't have a layer.
135  const SdfPrimSpecHandle GetPrimSpec() const
136  {
137  if (!_layer) {
138  return SdfPrimSpecHandle();
139  }
140  return _layer->GetPrimAtPath(_objectPath);
141  }
142 
143  /// Returns the SdfLayerHandle associated with this
144  /// UsdValidationValidatorErrorSite
145  const SdfLayerHandle &GetLayer() const
146  {
147  return _layer;
148  }
149 
150  /// Returns the UsdStage associated with this UsdValidationErrorSite;
151  /// nullptr othewrise.
152  const UsdStagePtr &GetStage() const
153  {
154  return _usdStage;
155  }
156 
157  /// Returns UsdPrim associated with this UsdValidationErrorSite, that is
158  /// when UsdStage is present and objectPath represents a prim path on this
159  /// stage; if not, an invalid prim is returned.
160  UsdPrim GetPrim() const
161  {
162  if (_usdStage) {
163  return _usdStage->GetPrimAtPath(_objectPath);
164  }
165  return UsdPrim();
166  }
167 
168  /// Returns UsdProperty associated with this UsdValidationErrorSite, that is
169  /// when UsdStage is present and objectPath represents a property path on
170  /// this stage; if not, an invalid property is returned.
172  {
173  if (_usdStage) {
174  return _usdStage->GetPropertyAtPath(_objectPath);
175  }
176  return UsdProperty();
177  }
178 
179  /// Returns true if \p other UsdValidationErrorSite has same valued members
180  /// as this UsdValidationErrorSite, false otherwise.
181  bool operator==(const UsdValidationErrorSite &other) const
182  {
183  return (_layer == other._layer) && (_usdStage == other._usdStage)
184  && (_objectPath == other._objectPath);
185  }
186 
187  /// Returns false if \p other UsdValidationErrorSite has same valued members
188  /// as this UsdValidationErrorSite, true otherwise.
189  bool operator!=(const UsdValidationErrorSite &other) const
190  {
191  return !(*this == other);
192  }
193 
194 private:
195  UsdStagePtr _usdStage;
196  SdfLayerHandle _layer;
197  SdfPath _objectPath;
198 
199 }; // UsdValidationErrorSite
200 
201 using UsdValidationErrorSites = std::vector<UsdValidationErrorSite>;
202 
203 /// \class UsdValidationError
204 ///
205 /// UsdValidationError is an entity returned by a validation task, which is
206 /// associated with a UsdValidationValidator.
207 ///
208 /// A UsdValidationError instance contains important information, like:
209 ///
210 /// - Name - A name the validator writer provided for the error. This is then
211 /// used to construct an identifier for the error.
212 ///
213 /// - UsdValidationErrorType - severity of an error,
214 ///
215 /// - UsdValidationErrorSites - on what sites validationError was reported by a
216 /// validation task,
217 ///
218 /// - Message - Message providing more information associated with the error.
219 /// Such a message is provided by the validator writer, when providing
220 /// implementation for the validation task function.
221 ///
222 /// - Data - Additional data associated with the error, which can be used by
223 /// the fixer(s) associated with the validator that generated the error.
224 ///
225 /// UsdValidationError instances are typically created by the validation task
226 /// functions, and returned as part of the vector of errors from a call to
227 /// UsdValidationValidator::Validate() function.
228 ///
229 /// A default constructed UsdValidationError instance signifies no error.
230 ///
231 /// A UsdValidationError instance contains a pointer to the
232 /// UsdValidationValidator that generated it, which can be retrieved using
233 /// GetValidator() method.
234 ///
235 /// A UsdValidationError instance can also provide access to the fixers
236 /// associated with the validator that generated it, which can be retrieved
237 /// using various GetFixer*() methods.
238 ///
240 {
241 public:
242  /// A default constructed UsdValidationError signifies no error.
245 
246  /// Instantiate a ValidationError by providing its \p name, \p errorType,
247  /// \p errorSites an \p errorMsg and optional \p data.
248  ///
251  const UsdValidationErrorType &errorType,
252  const UsdValidationErrorSites &errorSites,
253  const std::string &errorMsg,
254  const VtValue &data = VtValue());
255 
256  bool operator==(const UsdValidationError &other) const
257  {
258  return (_name == other._name) && (_errorType == other._errorType)
259  && (_errorSites == other._errorSites)
260  && (_errorMsg == other._errorMsg)
261  && (_validator == other._validator);
262  }
263 
264  bool operator!=(const UsdValidationError &other) const
265  {
266  return !(*this == other);
267  }
268 
269  /// Returns the name token of the UsdValidationError
270  const TfToken &GetName() const &
271  {
272  return _name;
273  }
274 
275  /// Returns the name token of the UsdValidationError by-value
277  {
278  return std::move(_name);
279  }
280 
281  /// Returns the UsdValidationErrorType associated with this
282  /// UsdValidationError
284  {
285  return _errorType;
286  }
287 
288  /// Returns the UsdValidationErrorSite associated with this
289  /// UsdValidationError
291  {
292  return _errorSites;
293  }
294 
295  /// Returns the UsdValidationErrorSite associated with this
296  /// UsdValidationError by-value
298  {
299  return std::move(_errorSites);
300  }
301 
302  /// Returns the UsdValidationValidator that reported this error.
303  ///
304  /// This will return nullptr if there is no UsdValidationValidator
305  /// associated with this error. This will never be nullptr for validation
306  /// errors returned from calls to UsdValidationValidator::Validate.
308  {
309  return _validator;
310  }
311 
312  /// Returns the message associated with this UsdValidationError
313  const std::string &GetMessage() const
314  {
315  return _errorMsg;
316  }
317 
318  /// Returns the data associated with this UsdValidationError
319  ///
320  /// Validator writers can provide additional data when creating a
321  /// UsdValidationError instance, which can then be retrieved using this
322  /// method, and may be used by the fixer associated with the validator.
323  const VtValue &GetData() const
324  {
325  return _data;
326  }
327 
328  /// An identifier for the error constructed from the validator name this
329  /// error was generated from and its name.
330  ///
331  /// Since a validator may result in multiple distinct errors, the identifier
332  /// helps in distinguishing and categorizing the errors. The identifier
333  /// returned will be in the following form:
334  /// For a plugin validator: "plugName":"validatorName"."ErrorName"
335  /// For a non-plugin validator: "validatorName"."ErrorName"
336  ///
337  /// For an error that was generated without a name, the identifier will be
338  /// same as the validator name which generated the error.
339  ///
340  /// For an error which is created directly and not via
341  /// UsdValidationValidator::Validate() call, we throw a coding error, as its
342  /// an improper use of the API.
344  TfToken GetIdentifier() const;
345 
346  /// Returns UsdValidationErrorType and ErrorMessage concatenated as a string
348  std::string GetErrorAsString() const;
349 
350  /// Returns true if UsdValidationErrorType is UsdValidationErrorType::None,
351  /// false otherwise
352  bool HasNoError() const
353  {
354  return _errorType == UsdValidationErrorType::None;
355  }
356 
357  /// Return a vector of fixers associated with this Validator.
358  ///
360  const std::vector<const UsdValidationFixer *> GetFixers() const;
361 
362  /// Return an immutable fixer given its \p name if it exists, else return
363  /// nullptr.
364  ///
366  const UsdValidationFixer *GetFixerByName(const TfToken &name) const;
367 
368  /// Return a vector of immutable fixers catering to a specific
369  /// \p errorName.
370  ///
372  const std::vector<const UsdValidationFixer *>
373  GetFixersByErrorName() const;
374 
375  /// Return an immutable fixer given its \p name and catering to a specific
376  /// error name if it exists, else return nullptr.
377  ///
380  const TfToken &name) const;
381 
382  /// Return a vector of immutable fixers catering to any of the given
383  /// \p keywords.
384  ///
385  /// Fixers can be associated with keywords, like unit, department, etc. which
386  /// can be used to filter fixers based on the context in which they are
387  /// being queried.
389  const std::vector<const UsdValidationFixer *> GetFixersByKeywords(
390  const TfTokenVector &keywords) const;
391 
392 
393 private:
394  // UsdValidationValidatorError holds a pointer to the UsdValidationValidator
395  // that generated it, so we need to provide friend access to allow the
396  // necessary mutation.
398 
399  // Used by UsdValidationValidator::Validate methods to embed itself to the
400  // reported errors.
401  void _SetValidator(const UsdValidationValidator *validator);
402 
403  // _validator is set when ValidationError is generated via a
404  // UsdValidationValidator::Validate() call.
405  const UsdValidationValidator *_validator;
406 
407  // These data members should not be modified other than during
408  // initialization by the validate task functions.
409  TfToken _name;
410  UsdValidationErrorType _errorType;
411  UsdValidationErrorSites _errorSites;
412  std::string _errorMsg;
413  VtValue _data;
414 
415 }; // UsdValidationError
416 
418 
419 #endif // PXR_USD_VALIDATION_USD_VALIDATION_ERROR_H
USDVALIDATION_API const std::vector< const UsdValidationFixer * > GetFixersByKeywords(const TfTokenVector &keywords) const
TfToken GetName()&&
Returns the name token of the UsdValidationError by-value.
Definition: error.h:276
bool HasNoError() const
Definition: error.h:352
USDVALIDATION_API TfToken GetIdentifier() const
UsdProperty GetProperty() const
Definition: error.h:171
UsdPrim GetPrim() const
Definition: error.h:160
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
bool IsValidSpecInLayer() const
Definition: error.h:94
const UsdStagePtr & GetStage() const
Definition: error.h:152
bool operator==(const UsdValidationError &other) const
Definition: error.h:256
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:405
bool IsProperty() const
Definition: error.h:111
UsdValidationErrorSites GetSites()&&
Definition: error.h:297
const SdfPrimSpecHandle GetPrimSpec() const
Definition: error.h:135
const TfToken & GetName() const &
Returns the name token of the UsdValidationError.
Definition: error.h:270
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:313
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:181
Definition: token.h:70
USDVALIDATION_API const std::vector< const UsdValidationFixer * > GetFixersByErrorName() const
bool operator!=(const UsdValidationErrorSite &other) const
Definition: error.h:189
const SdfLayerHandle & GetLayer() const
Definition: error.h:145
USDVALIDATION_API const std::vector< const UsdValidationFixer * > GetFixers() const
const UsdValidationErrorSites & GetSites() const &
Definition: error.h:290
USDVALIDATION_API std::string GetErrorAsString() const
Returns UsdValidationErrorType and ErrorMessage concatenated as a string.
Definition: prim.h:116
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
bool IsValid() const
Definition: error.h:87
GLuint const GLchar * name
Definition: glcorearb.h:786
USDVALIDATION_API const UsdValidationFixer * GetFixerByNameAndErrorName(const TfToken &name) const
Definition: path.h:280
std::vector< UsdValidationErrorSite > UsdValidationErrorSites
Definition: error.h:201
const VtValue & GetData() const
Definition: error.h:323
bool IsPrim() const
Definition: error.h:104
bool IsValid() const
Return true if this is a valid object, false otherwise.
Definition: object.h:127
USDVALIDATION_API const UsdValidationFixer * GetFixerByName(const TfToken &name) const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
const UsdValidationValidator * GetValidator() const
Definition: error.h:307
const SdfPropertySpecHandle GetPropertySpec() const
Definition: error.h:122
UsdValidationErrorType GetType() const
Definition: error.h:283
UsdValidationErrorSite()=default
bool operator!=(const UsdValidationError &other) const
Definition: error.h:264
Definition: value.h:89
Definition: format.h:1821