Mantid
Loading...
Searching...
No Matches
ErrorReporter.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8#include "MantidJson/Json.h"
15#include "MantidKernel/Logger.h"
17
18#include <Poco/ActiveResult.h>
19#include <json/json.h>
20
21#include <utility>
22
23namespace Mantid::Kernel {
24
25namespace {
27Logger g_log("ErrorReporter");
28} // namespace
29
30//----------------------------------------------------------------------------------------------
31// Constructor for ErrorReporter
34ErrorReporter::ErrorReporter(const std::string &application, const Types::Core::time_duration &upTime,
35 const std::string &exitCode, const bool share)
36 : ErrorReporter(application, upTime, exitCode, share, "", "", "", "") {}
37
40ErrorReporter::ErrorReporter(const std::string &application, const Types::Core::time_duration &upTime,
41 const std::string &exitCode, const bool share, const std::string &name,
42 const std::string &email, const std::string &textBox)
43 : ErrorReporter(application, upTime, exitCode, share, name, email, textBox, "") {}
44
45ErrorReporter::ErrorReporter(std::string application, Types::Core::time_duration upTime, std::string exitCode,
46 const bool share, std::string name, std::string email, std::string textBox,
47 std::string traceback)
48 : m_application(std::move(application)), m_exitCode(std::move(exitCode)), m_upTime(std::move(upTime)),
49 m_share(share), m_name(std::move(name)), m_email(std::move(email)), m_textbox(std::move(textBox)),
50 m_stacktrace(std::move(traceback)) {
51 auto url = Mantid::Kernel::ConfigService::Instance().getValue<std::string>("errorreports.rooturl");
52 if (!url.is_initialized()) {
53 g_log.debug() << "Failed to load error report url\n";
54 } else {
55 m_url = url.get();
56 }
57}
58
62 try {
63 std::string message = this->generateErrorMessage();
64
65 // send the report
66 // Poco::ActiveResult<int> result = m_errorActiveMethod(message);
67 const auto status = this->sendReport(message, m_url + "/api/error");
68 return status;
70 g_log.debug() << "Send error report failure. " << ex.what() << '\n';
71 return static_cast<InternetHelper::HTTPStatus>(ex.errorCode());
72 } catch (std::exception &ex) {
73 g_log.debug() << "Send error report failure. " << ex.what() << '\n';
75 }
76}
77
81 ::Json::Value message;
82
83 // username
84 message["uid"] = Kernel::ChecksumHelper::md5FromString(ConfigService::Instance().getUsername());
85 // hostname
86 message["host"] = Kernel::ChecksumHelper::md5FromString(ConfigService::Instance().getComputerName());
87
88 // os name, version, and architecture
89 message["osName"] = ConfigService::Instance().getOSName();
90 message["osArch"] = ConfigService::Instance().getOSArchitecture();
91 message["osVersion"] = ConfigService::Instance().getOSVersion();
92 message["osReadable"] = ConfigService::Instance().getOSVersionReadable();
93
94 // legacy interface requires paraview version DON'T REMOVE
95 message["ParaView"] = 0;
96
97 // mantid version and sha1
98 message["mantidVersion"] = MantidVersion::version();
99 message["mantidSha1"] = MantidVersion::revisionFull();
100
101 message["dateTime"] = Types::Core::DateAndTime::getCurrentTime().toISO8601String();
102
103 message["upTime"] = to_simple_string(m_upTime);
104
105 message["application"] = m_application;
106
107 message["facility"] = ConfigService::Instance().getFacility().name();
108
109 message["exitCode"] = m_exitCode;
110
111 if (m_share) {
112 message["textBox"] = m_textbox;
113 message["email"] = m_email;
114 message["name"] = m_name;
115 message["stacktrace"] = m_stacktrace;
116 } else {
117 message["email"] = "";
118 message["name"] = "";
119 message["textBox"] = m_textbox;
120 message["stacktrace"] = "";
121 }
122
123 return Mantid::JsonHelpers::jsonToString(message);
124}
125
130Kernel::InternetHelper::HTTPStatus ErrorReporter::sendReport(const std::string &message, const std::string &url) {
132 try {
134 std::stringstream responseStream;
135 helper.setTimeout(20);
136 helper.setBody(message);
137 status = helper.sendRequest(url, responseStream);
139 status = static_cast<InternetHelper::HTTPStatus>(e.errorCode());
140 g_log.information() << "Call to \"" << url << "\" responded with " << static_cast<int>(status) << "\n"
141 << e.what() << "\n";
142 }
143
144 return status;
145}
146
147} // namespace Mantid::Kernel
ErrorReporter : The error reporter is responsible for sending error reports.
Definition: ErrorReporter.h:22
virtual Kernel::InternetHelper::HTTPStatus sendReport(const std::string &message, const std::string &url)
Sends report using Internet Helper.
const std::string m_application
Definition: ErrorReporter.h:44
const std::string m_exitCode
Definition: ErrorReporter.h:45
ErrorReporter(const std::string &application, const Types::Core::time_duration &startTime, const std::string &exitCode, bool share)
Constructor.
Kernel::InternetHelper::HTTPStatus sendErrorReport()
Sends an error report.
virtual std::string generateErrorMessage() const
Generates an error string in json format.
const std::string m_textbox
Definition: ErrorReporter.h:50
const Types::Core::time_duration m_upTime
Definition: ErrorReporter.h:46
const std::string m_stacktrace
Definition: ErrorReporter.h:52
Exception thrown when error occurs accessing an internet resource.
Definition: Exception.h:321
const char * what() const noexcept override
Overloaded reporting method.
Definition: Exception.cpp:311
const int & errorCode() const
Writes out the range and limits.
Definition: Exception.cpp:317
InternetHelper : A helper class for supporting access to resources through HTTP and HTTPS.
void setBody(const std::string &body)
Sets the body & content length for future requests, this will also set the method to POST is the body...
void setTimeout(int seconds)
Sets the timeout in seconds.
virtual HTTPStatus sendRequest(const std::string &url, std::ostream &responseStream)
Performs a request using http or https depending on the url.
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
static const char * version()
The full version number.
static const char * revisionFull()
The full SHA-1 of the last commit.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
MANTID_KERNEL_DLL std::string md5FromString(const std::string &input)
create a md5 checksum from a string
STL namespace.