| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | #include <sstream> |
| 3 | #include <optional> |
| 4 | #include "RuntimeErrorWithSourceLocation.h" |
| 5 | |
| 6 | RuntimeErrorWithSourceLocation::RuntimeErrorWithSourceLocation(const std::string& reason, const std::source_location& location) : |
| 7 | std::runtime_error(BuildMessage(reason, {}, location)) |
| 8 | { |
| 9 | } |
| 10 | |
| 11 | RuntimeErrorWithSourceLocation::RuntimeErrorWithSourceLocation(const std::string& reason, const std::exception& inner, const std::source_location& location) : |
| 12 | std::runtime_error(BuildMessage(reason, inner, location)) |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | std::string RuntimeErrorWithSourceLocation::BuildMessage(const std::string& reason, const std::optional<std::exception>& inner, const std::source_location& source) |
| 17 | { |
| 18 | std::stringstream str; |
| 19 | str << "Exception thrown by " << source.function_name() << " in " << source.file_name() << ":" << source.line() << ":" << reason; |
| 20 | if (inner.has_value()) |
| 21 | { |
| 22 | str << "\nInner exception: " << inner.value().what(); |
| 23 | } |
| 24 | |
| 25 | return str.str(); |
| 26 | } |