mirror of https://github.com/Icinga/icinga2.git
Update error messages
Removes verboseError from httprequest and uses HttpUtility::GetLastParameter() instead to find out whether verbose errors are enabled. Also parsing an invalid URL will now not lead to a stacktrace anymore. refs #10194
This commit is contained in:
parent
2fc042e231
commit
edfc0e3a38
|
@ -66,7 +66,7 @@ bool ActionsHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& reques
|
|||
} catch (const std::exception& ex) {
|
||||
HttpUtility::SendJsonError(response, 400,
|
||||
"Type/Filter was required but not provided or was invalid.",
|
||||
request.GetVerboseErrors() ? DiagnosticInformation(ex) : "");
|
||||
HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
|
@ -86,7 +86,7 @@ bool ActionsHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& reques
|
|||
Dictionary::Ptr fail = new Dictionary();
|
||||
fail->Set("code", 500);
|
||||
fail->Set("status", "Action execution failed.");
|
||||
if (request.GetVerboseErrors())
|
||||
if (HttpUtility::GetLastParameter(params, "verboseErrors"))
|
||||
fail->Set("diagnostic information", DiagnosticInformation(ex));
|
||||
results->Add(fail);
|
||||
}
|
||||
|
|
|
@ -88,7 +88,7 @@ bool ConfigFilesHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& re
|
|||
response.WriteBody(content.CStr(), content.GetLength());
|
||||
} catch (const std::exception& ex) {
|
||||
HttpUtility::SendJsonError(response, 500, "Could not read file.",
|
||||
request.GetVerboseErrors() ? DiagnosticInformation(ex) : "");
|
||||
HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
@ -90,7 +90,7 @@ void ConfigPackagesHandler::HandlePost(const ApiUser::Ptr& user, HttpRequest& re
|
|||
ConfigPackageUtility::CreatePackage(packageName);
|
||||
} catch (const std::exception& ex) {
|
||||
HttpUtility::SendJsonError(response, 500, "Could not create package.",
|
||||
request.GetVerboseErrors() ? DiagnosticInformation(ex) : "");
|
||||
HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
|
||||
}
|
||||
|
||||
result1->Set("code", 200);
|
||||
|
@ -131,7 +131,7 @@ void ConfigPackagesHandler::HandleDelete(const ApiUser::Ptr& user, HttpRequest&
|
|||
} catch (const std::exception& ex) {
|
||||
code = 500;
|
||||
status = "Failed to delete package.";
|
||||
if (request.GetVerboseErrors())
|
||||
if (HttpUtility::GetLastParameter(params, "verboseErrors"))
|
||||
result1->Set("diagnostic information", DiagnosticInformation(ex));
|
||||
}
|
||||
|
||||
|
|
|
@ -118,7 +118,7 @@ void ConfigStagesHandler::HandlePost(const ApiUser::Ptr& user, HttpRequest& requ
|
|||
} catch (const std::exception& ex) {
|
||||
return HttpUtility::SendJsonError(response, 500,
|
||||
"Stage creation failed.",
|
||||
request.GetVerboseErrors() ? DiagnosticInformation(ex) : "");
|
||||
HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
|
||||
}
|
||||
|
||||
Dictionary::Ptr result1 = new Dictionary();
|
||||
|
@ -164,7 +164,7 @@ void ConfigStagesHandler::HandleDelete(const ApiUser::Ptr& user, HttpRequest& re
|
|||
} catch (const std::exception& ex) {
|
||||
return HttpUtility::SendJsonError(response, 500,
|
||||
"Failed to delete stage.",
|
||||
request.GetVerboseErrors() ? DiagnosticInformation(ex) : "");
|
||||
HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
|
||||
}
|
||||
|
||||
Dictionary::Ptr result1 = new Dictionary();
|
||||
|
|
|
@ -34,8 +34,7 @@ HttpRequest::HttpRequest(const Stream::Ptr& stream)
|
|||
ProtocolVersion(HttpVersion11),
|
||||
Headers(new Dictionary()),
|
||||
m_Stream(stream),
|
||||
m_State(HttpRequestStart),
|
||||
verboseErrors(false)
|
||||
m_State(HttpRequestStart)
|
||||
{ }
|
||||
|
||||
bool HttpRequest::Parse(StreamReadContext& src, bool may_wait)
|
||||
|
@ -61,7 +60,6 @@ bool HttpRequest::Parse(StreamReadContext& src, bool may_wait)
|
|||
|
||||
RequestMethod = tokens[0];
|
||||
RequestUrl = new class Url(tokens[1]);
|
||||
verboseErrors = (RequestUrl->GetQueryElement("verboseErrors") == "true");
|
||||
|
||||
if (tokens[2] == "HTTP/1.0")
|
||||
ProtocolVersion = HttpVersion10;
|
||||
|
|
|
@ -69,15 +69,11 @@ public:
|
|||
void WriteBody(const char *data, size_t count);
|
||||
void Finish(void);
|
||||
|
||||
inline bool GetVerboseErrors(void)
|
||||
{ return verboseErrors; }
|
||||
|
||||
private:
|
||||
Stream::Ptr m_Stream;
|
||||
boost::shared_ptr<ChunkReadContext> m_ChunkContext;
|
||||
HttpRequestState m_State;
|
||||
FIFO::Ptr m_Body;
|
||||
bool verboseErrors;
|
||||
|
||||
void FinishHeaders(void);
|
||||
};
|
||||
|
|
|
@ -86,6 +86,15 @@ bool HttpServerConnection::ProcessMessage(void)
|
|||
|
||||
try {
|
||||
res = m_CurrentRequest.Parse(m_Context, false);
|
||||
} catch (const std::invalid_argument& ex) {
|
||||
HttpResponse response(m_Stream, m_CurrentRequest);
|
||||
response.SetStatus(400, "Bad request");
|
||||
String msg = String("<h1>Bad request</h1><p><pre>") + ex.what() + "</pre></p>";
|
||||
response.WriteBody(msg.CStr(), msg.GetLength());
|
||||
response.Finish();
|
||||
|
||||
m_Stream->Shutdown();
|
||||
return false;
|
||||
} catch (const std::exception& ex) {
|
||||
HttpResponse response(m_Stream, m_CurrentRequest);
|
||||
response.SetStatus(400, "Bad request");
|
||||
|
|
Loading…
Reference in New Issue