Skip to content

Commit eed1aa9

Browse files
adlarkinTeo Koon PengahcordeAddisu Z. Taddese
authored
Add USDError class (#836)
Signed-off-by: Ashton Larkin <42042756+adlarkin@users.noreply.github.com> Co-authored-by: Teo Koon Peng <koonpeng@openrobotics.org> Co-authored-by: Alejandro Hernández Cordero <ahcorde@gmail.com> Co-authored-by: Addisu Z. Taddese <addisu@openrobotics.org>
1 parent 8e832e7 commit eed1aa9

8 files changed

Lines changed: 508 additions & 14 deletions

File tree

usd/include/sdf/usd/UsdError.hh

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright 2022 Open Source Robotics Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
#ifndef SDF_USD_USDERROR_HH_
18+
#define SDF_USD_USDERROR_HH_
19+
20+
#include <optional>
21+
#include <string>
22+
#include <vector>
23+
24+
#include <ignition/utils/ImplPtr.hh>
25+
26+
#include <sdf/Error.hh>
27+
#include <sdf/usd/Export.hh>
28+
#include <sdf/config.hh>
29+
30+
namespace sdf
31+
{
32+
// Inline bracket to help doxygen filtering.
33+
inline namespace SDF_VERSION_NAMESPACE
34+
{
35+
namespace usd
36+
{
37+
//
38+
/// \enum ErrorCode
39+
/// \brief Set of error codes. Usually one or more errors are returned in
40+
/// an Errors vector. The collection of Errors should be taken as a whole,
41+
/// where an error toward the beginning of the vector can inform errors
42+
/// toward the end of the vector.
43+
/// \sa Errors
44+
enum class UsdErrorCode
45+
{
46+
// \brief No error
47+
NONE = 0,
48+
49+
/// \brief A wrapped SDF error.
50+
SDF_ERROR,
51+
52+
/// \brief Parsing of SDF object to a USD object failed.
53+
SDF_TO_USD_PARSING_ERROR,
54+
55+
/// \brief The pxr::SdfPath does not point to a valid USD prim.
56+
INVALID_PRIM_PATH,
57+
58+
/// \brief A pxr API was not able to be applied to a USD prim.
59+
FAILED_PRIM_API_APPLY,
60+
};
61+
62+
class IGNITION_SDFORMAT_USD_VISIBLE UsdError
63+
{
64+
/// \brief Default constructor
65+
public: UsdError();
66+
67+
/// \brief Constructor.
68+
/// \param[in] _code The error code. If the error code is SDF_ERROR, the
69+
/// constructor that takes an sdf::Error object should be used instead.
70+
/// \param[in] _message A description of the error.
71+
/// \sa ErrorCode.
72+
public: UsdError(const UsdErrorCode _code, const std::string &_message);
73+
74+
/// \brief Constructor.
75+
/// \param[in] _code The error code. If the error code is SDF_ERROR, the
76+
/// constructor that takes an sdf::Error object should be used instead.
77+
/// \param[in] _message A description of the error.
78+
/// \param[in] _filePath The file path that is related to this error.
79+
/// \sa ErrorCode.
80+
public: UsdError(const UsdErrorCode _code, const std::string &_message,
81+
const std::string &_filePath);
82+
83+
/// \brief Constructor.
84+
/// \param[in] _code The error code. If the error code is SDF_ERROR, the
85+
/// constructor that takes an sdf::Error object should be used instead.
86+
/// \param[in] _message A description of the error.
87+
/// \param[in] _filePath The file path that is related to this error.
88+
/// \param[in] _lineNumber The line number in the provided file path where
89+
/// this error was raised.
90+
/// \sa ErrorCode.
91+
public: UsdError(const UsdErrorCode _code, const std::string &_message,
92+
const std::string &_filePath, int _lineNumber);
93+
94+
/// \brief Constructor.
95+
/// \param[in] _sdf_error Wrap an sdf error.
96+
/// \sa ErrorCode.
97+
public: explicit UsdError(const sdf::Error &_sdfError);
98+
99+
/// \brief Get the error code.
100+
/// \return An error code.
101+
/// \sa ErrorCode.
102+
public: UsdErrorCode Code() const;
103+
104+
/// \brief Get the error message, which is a description of the error.
105+
/// \return Error message.
106+
public: const std::string &Message() const;
107+
108+
/// \brief Get the file path associated with this error.
109+
/// \return Returns the path of the file that this error is related to,
110+
/// nullopt otherwise.
111+
public: const std::optional<std::string> &FilePath() const;
112+
113+
/// \brief Sets the file path that is associated with this error.
114+
/// \param[in] _filePath The file path that is related to this error. (e.g.
115+
/// /tmp/test_file.usd)
116+
public: void SetFilePath(const std::string &_filePath);
117+
118+
/// \brief Get the line number associated with this error.
119+
/// \return Returns the line number. nullopt otherwise.
120+
public: std::optional<int> LineNumber() const;
121+
122+
/// \brief Sets the line number that is associated with this error.
123+
/// \param[in] _lineNumber The line number that is related to this error.
124+
public: void SetLineNumber(int _lineNumber);
125+
126+
/// \brief Get the underlying sdf error.
127+
/// \return The underlying sdf error or nullopt otherwise.
128+
public: std::optional<sdf::Error> SdfError() const;
129+
130+
/// \brief Safe bool conversion.
131+
/// \return True if this Error's Code() != NONE. In otherwords, this is
132+
/// true when there is an error.
133+
public: explicit operator bool() const;
134+
135+
/// \brief Compare this Error to a boolean value.
136+
/// \return True if the boolean evaluation of this Error equals _value.
137+
/// If _value == false, then true is returned when this Error's Code() is
138+
/// equal to NONE and false is returned otherwise. If _value == true,
139+
/// then true is returned when this Error's Code() is not equal to NONE
140+
/// and false is returned otherwise.
141+
/// \sa explicit operator bool() const
142+
public: bool operator==(const bool _value) const;
143+
144+
/// \brief Output operator for an error.
145+
/// \param[in,out] _out The output stream.
146+
/// \param[in] _err The error to output.
147+
/// \return Reference to the given output stream
148+
public: friend IGNITION_SDFORMAT_VISIBLE std::ostream &operator<<(
149+
std::ostream &_out, const sdf::usd::UsdError &_err);
150+
151+
/// \brief Private data pointer.
152+
IGN_UTILS_IMPL_PTR(dataPtr)
153+
};
154+
155+
using UsdErrors = std::vector<UsdError>;
156+
157+
} // namespace usd
158+
} // namespace SDF_VERSION_NAMESPACE
159+
} // namespace sdf
160+
161+
#ifdef _WIN32
162+
#pragma warning(pop)
163+
#endif
164+
165+
#endif

usd/include/sdf/usd/sdf_parser/Light.hh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "sdf/config.hh"
3333
#include "sdf/usd/Export.hh"
34+
#include "sdf/usd/UsdError.hh"
3435
#include "sdf/Light.hh"
3536

3637
namespace sdf
@@ -46,9 +47,9 @@ namespace sdf
4647
/// of _light.
4748
/// \param[in] _path The USD path of the parsed light in _stage, which must
4849
/// be a valid USD path.
49-
/// \return Errors, which is a vector of Error objects. Each Error includes
50-
/// an error code and message. An empty vector indicates no error.
51-
sdf::Errors IGNITION_SDFORMAT_USD_VISIBLE ParseSdfLight(
50+
/// \return UsdErrors, which is a vector of Error objects. Each Error
51+
/// includes an error code and message. An empty vector indicates no error.
52+
UsdErrors IGNITION_SDFORMAT_USD_VISIBLE ParseSdfLight(
5253
const sdf::Light &_light,
5354
pxr::UsdStageRefPtr &_stage,
5455
const std::string &_path);

usd/include/sdf/usd/sdf_parser/World.hh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "sdf/config.hh"
3333
#include "sdf/usd/Export.hh"
34+
#include "sdf/usd/UsdError.hh"
3435
#include "sdf/World.hh"
3536

3637
namespace sdf
@@ -44,11 +45,11 @@ namespace sdf
4445
/// \param[in] _world The SDF world to parse.
4546
/// \param[in] _stage The stage that should contain the USD representation
4647
/// of _world. It must be initialized first
47-
/// \param[in] _path The USD path of the parsed world in _stage, which must be
48-
/// a valid USD path.
49-
/// \return Errors, which is a vector of Error objects. Each Error includes
50-
/// an error code and message. An empty vector indicates no error.
51-
sdf::Errors IGNITION_SDFORMAT_USD_VISIBLE ParseSdfWorld(
48+
/// \param[in] _path The USD path of the parsed world in _stage, which must
49+
/// be a valid USD path.
50+
/// \return UsdErrors, which is a vector of UsdError objects. Each UsdError
51+
/// includes an error code and message. An empty vector indicates no error.
52+
UsdErrors IGNITION_SDFORMAT_USD_VISIBLE ParseSdfWorld(
5253
const sdf::World &_world,
5354
pxr::UsdStageRefPtr &_stage,
5455
const std::string &_path);

usd/src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(sources
2+
UsdError.cc
23
sdf_parser/Light.cc
34
sdf_parser/World.cc
45
)
@@ -20,6 +21,7 @@ set(gtest_sources
2021
sdf_parser/sdf2usd_TEST.cc
2122
sdf_parser/Light_Sdf2Usd_TEST.cc
2223
sdf_parser/World_Sdf2Usd_TEST.cc
24+
UsdError_TEST.cc
2325
)
2426

2527
# Build the unit tests

0 commit comments

Comments
 (0)