RStudio Launcher Plugin SDK  1.1.3
A software development kit for creating plugins that work the the RStudio Launcher.
Error.hpp
1 /*
2  * Error.hpp
3  *
4  * Copyright (C) 2022 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant to the terms of a commercial license agreement
7  * with RStudio, then this program is licensed to you under the following terms:
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
10  * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
11  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #ifndef LAUNCHER_PLUGINS_ERROR_HPP
25 #define LAUNCHER_PLUGINS_ERROR_HPP
26 
27 #include <string>
28 #include <system_error>
29 #include <vector>
30 
31 #include <PImpl.hpp>
32 #include <logging/Logger.hpp>
33 
34 namespace rstudio {
35 namespace launcher_plugins {
36 namespace system {
37 
38 class FilePath;
39 
40  } // namespace system
41 
42 
43 class Error;
44 class Success;
45 
49 class ErrorLock
50 {
51  friend class Error;
52  friend class Success;
53 
57  virtual ~ErrorLock() = default;
58 
59 private:
63  ErrorLock() = default;
64 
68  ErrorLock(const ErrorLock&) = default;
69 };
70 
75 {
76 public:
80  ErrorLocation();
81 
87  ErrorLocation(const ErrorLocation& in_other);
88 
94  ErrorLocation(ErrorLocation&& in_other) noexcept;
95 
103  ErrorLocation(const char* in_function, const char* in_file, long in_line);
104 
112  ErrorLocation& operator=(const ErrorLocation& in_other);
113 
121  bool operator==(const ErrorLocation& in_location) const;
122 
128  std::string asString() const;
129 
135  const std::string& getFile() const;
136 
142  const std::string& getFunction() const;
143 
149  long getLine() const;
150 
156  bool hasLocation() const;
157 
158 private:
159  // The private implementation of ErrorLocation.
160  PRIVATE_IMPL(m_impl);
161 };
162 
166 typedef std::vector<std::pair<std::string, std::string> > ErrorProperties;
167 
174 class Error : public virtual ErrorLock
175 {
176 public:
180  Error();
181 
187  Error(const Error& in_other);
188 
196  Error(std::string in_name, int in_code, const ErrorLocation& in_location);
197 
206  Error(std::string in_name, int in_code, const Error& in_cause, const ErrorLocation& in_location);
207 
217  Error(std::string in_name, int in_code, std::string in_message, const ErrorLocation& in_location);
218 
229  Error(std::string in_name,
230  int in_code,
231  std::string in_message,
232  const Error& in_cause,
233  const ErrorLocation& in_location);
234 
238  ~Error() override = default;
239 
245  explicit operator bool() const;
246 
252  bool operator!() const;
253 
261  bool operator==(const Error& in_other) const;
262 
270  bool operator!=(const Error& in_other) const;
271 
279  void addOrUpdateProperty(const std::string& in_name, const std::string& in_value);
280 
288  void addOrUpdateProperty(const std::string& in_name, const system::FilePath& in_value);
289 
297  void addOrUpdateProperty(const std::string& in_name, int in_value);
298 
305  void addProperty(const std::string& in_name, const std::string& in_value);
306 
313  void addProperty(const std::string& in_name, const system::FilePath& in_value);
314 
321  void addProperty(const std::string& in_name, int in_value);
322 
328  std::string asString() const;
329 
335  bool hasCause() const;
336 
342  const Optional<Error>& getCause() const;
343 
349  int getCode() const;
350 
356  const ErrorLocation& getLocation() const;
357 
363  const std::string& getMessage() const;
364 
370  const std::string& getName() const;
371 
377  const ErrorProperties& getProperties() const;
378 
386  std::string getProperty(const std::string& name) const;
387 
393  std::string getSummary() const;
394 
400  bool isExpected() const;
401 
407  void setExpected();
408 
409 private:
413  void copyOnWrite();
414 
421  bool isError() const;
422 
423  // The private implementation of Error.
424  PRIVATE_IMPL_SHARED(m_impl);
425 
431  Impl& impl() const;
432 };
433 
437 class Success : public Error
438 {
439 public:
443  Success() : Error() {}
444 };
445 
454 std::ostream& operator<<(std::ostream& io_ostream, const Error& in_error);
455 
464 Error systemError(int in_code, const ErrorLocation& in_location);
465 
474 Error systemError(const std::error_code& in_code, const ErrorLocation& in_location);
475 
484 Error systemError(const std::system_error& in_error, const ErrorLocation& in_location);
485 
495 Error systemError(int in_code, const Error& in_cause, const ErrorLocation& in_location);
496 
506 Error systemError(const std::error_code& in_code, const Error& in_cause, const ErrorLocation& in_location);
507 
517 Error systemError(const std::system_error& in_error, const Error& in_cause, const ErrorLocation& in_location);
518 
529 Error systemError(int in_code, const std::string& in_description, const ErrorLocation& in_location);
530 
541 Error systemError(const std::error_code& in_code, const std::string& in_description, const ErrorLocation& in_location);
542 
553 Error systemError(
554  const std::system_error& in_error,
555  const std::string& in_description,
556  const ErrorLocation& in_location);
557 
569 Error systemError(
570  int in_code,
571  const std::string& in_description,
572  const Error& in_cause,
573  const ErrorLocation& in_location);
574 
586 Error systemError(
587  const std::error_code& in_code,
588  const std::string& in_description,
589  const Error& in_cause,
590  const ErrorLocation& in_location);
591 
603 Error systemError(
604  const std::system_error& in_error,
605  const std::string& in_description,
606  const Error& in_cause,
607  const ErrorLocation& in_location);
608 
618 Error systemCallError(
619  const std::string& in_function,
620  int in_code,
621  const ErrorLocation& in_location);
622 
633 Error systemCallError(
634  const std::string& in_function,
635  int in_code,
636  const std::string& in_message,
637  const ErrorLocation& in_location);
638 
650 Error unknownError(const std::string& in_message, const ErrorLocation& in_location);
651 
663 Error unknownError(const std::string& in_message, const Error& in_cause, const ErrorLocation& in_location);
664 
665 } // namespace launcher_plugins
666 } // namespace rstudio
667 
668 #ifdef STRIPPED_FILENAME
669 #define ERROR_LOCATION rstudio::launcher_plugins::ErrorLocation( \
670  __FUNCTION__, STRIPPED_FILENAME, __LINE__)
671 #else
672 #define ERROR_LOCATION rstudio::launcher_plugins::ErrorLocation( \
673  __FUNCTION__, __FILE__, __LINE__)
674 #endif
675 
676 #define CATCH_UNEXPECTED_EXCEPTION \
677  catch(const std::exception& e) \
678  { \
679  rstudio::launcher_plugins::logging::logErrorMessage(std::string("Unexpected exception: ") + \
680  e.what(), ERROR_LOCATION); \
681  } \
682  catch(...) \
683  { \
684  rstudio::launcher_plugins::logging::logErrorMessage("Unknown exception", ERROR_LOCATION); \
685  }
686 
687 #endif
rstudio::launcher_plugins::Error::getName
const std::string & getName() const
Gets the name of the error.
rstudio::launcher_plugins::system::FilePath
Class which represents a path on the system. May be any type of file (e.g. directory,...
Definition: FilePath.hpp:77
rstudio::launcher_plugins::ErrorLocation::ErrorLocation
ErrorLocation()
Default constructor.
rstudio::launcher_plugins::ErrorLocation
Class which represents the location of an error.
Definition: Error.hpp:74
rstudio::launcher_plugins::Success::Success
Success()
Constructor.
Definition: Error.hpp:443
rstudio::launcher_plugins::Error::isExpected
bool isExpected() const
Gets whether this error was expected or not.
rstudio::launcher_plugins::Error::Error
Error()
Constructor.
rstudio::launcher_plugins::Error::operator!=
bool operator!=(const Error &in_other) const
Inequality operator. Two errors are equal if their codes and names are the same.
rstudio::launcher_plugins::Error::hasCause
bool hasCause() const
Checks whether this error was caused by a separate error.
rstudio::launcher_plugins::Error::addProperty
void addProperty(const std::string &in_name, const std::string &in_value)
Adds a property of this error. If a property with the same name already exists, a duplicate will be a...
rstudio::launcher_plugins::Error::setExpected
void setExpected()
Sets the property that indicates that this error was expected. Errors are unexpected by default; only...
rstudio::launcher_plugins::Error::addOrUpdateProperty
void addOrUpdateProperty(const std::string &in_name, const std::string &in_value)
Add or updates a property of this error. If any properties with the specified name exist,...
rstudio::launcher_plugins::Error::asString
std::string asString() const
Formats the error as a string.
rstudio::launcher_plugins::Success
Class which represents a successful operation (i.e. no error).
Definition: Error.hpp:437
rstudio::launcher_plugins::Error::getSummary
std::string getSummary() const
Gets the cause of the error.
rstudio::launcher_plugins::Error::getMessage
const std::string & getMessage() const
Gets the error message.
rstudio::launcher_plugins::ErrorLocation::operator=
ErrorLocation & operator=(const ErrorLocation &in_other)
Assignment operator.
rstudio::launcher_plugins::ErrorLocation::operator==
bool operator==(const ErrorLocation &in_location) const
Equality comparison operator.
rstudio::launcher_plugins::Error::getProperties
const ErrorProperties & getProperties() const
Gets the custom properties of the error.
rstudio::launcher_plugins::ErrorLock
A class which can be derived from in order to prevent child classes from being derived from further.
Definition: Error.hpp:49
rstudio::launcher_plugins::ErrorLocation::getLine
long getLine() const
Gets the line where the error occurred.
rstudio::launcher_plugins::Error::getProperty
std::string getProperty(const std::string &name) const
Gets a custom property of this error.
rstudio::launcher_plugins::ErrorLocation::hasLocation
bool hasLocation() const
Checks whether the location is set.
rstudio::launcher_plugins::Error
Class which represents an error.
Definition: Error.hpp:174
Logger.hpp
rstudio::launcher_plugins::Error::getCode
int getCode() const
Gets the error code.
rstudio::launcher_plugins::ErrorLocation::getFile
const std::string & getFile() const
Gets the file where the error occurred.
rstudio::launcher_plugins::Error::operator==
bool operator==(const Error &in_other) const
Equality operator. Two errors are equal if their codes and names are the same.
rstudio::launcher_plugins::Error::getCause
const Optional< Error > & getCause() const
Gets the error which caused this error.
rstudio::launcher_plugins::Optional
Container class which represents a value that may or may not be set.
Definition: Optional.hpp:38
rstudio::launcher_plugins::ErrorLocation::getFunction
const std::string & getFunction() const
Gets the function where the error occurred.
rstudio::launcher_plugins::ErrorLocation::asString
std::string asString() const
Formats the error location as a string.
rstudio::launcher_plugins::Error::~Error
~Error() override=default
Non-virtual destructor because only Success inherits Error and it will keep Error lightweight.
rstudio::launcher_plugins::Error::operator!
bool operator!() const
Overloaded operator ! to allow Errors to be treated as boolean values.
rstudio::launcher_plugins::Error::getLocation
const ErrorLocation & getLocation() const
Gets the location where the error occurred.