Try to make the json ui more tolerant with the json input

Should improve the issues raised #714
This commit is contained in:
Maxime Coste 2016-06-29 21:08:16 +01:00
parent 511367f977
commit c8f5204202

View File

@ -1,5 +1,6 @@
#include "json_ui.hh" #include "json_ui.hh"
#include "containers.hh"
#include "display_buffer.hh" #include "display_buffer.hh"
#include "keys.hh" #include "keys.hh"
#include "file.hh" #include "file.hh"
@ -423,34 +424,32 @@ void JsonUI::parse_requests(EventMode mode)
if (not m_requests.empty()) if (not m_requests.empty())
{ {
Value json; const char* pos = nullptr;
const char* pos;
try try
{ {
Value json;
std::tie(json, pos) = parse_json(m_requests); std::tie(json, pos) = parse_json(m_requests);
if (json)
eval_json(json);
} }
catch (runtime_error& error) catch (runtime_error& error)
{ {
write_stderr(format("error while parsing requests '{}': '{}'", write_stderr(format("error while handling requests '{}': '{}'",
m_requests, error.what())); m_requests, error.what()));
}
if (json) // try to salvage request by dropping its first line
{ auto eol = find(m_requests, '\n');
try if (eol != m_requests.end())
{ m_requests = String{eol+1, m_requests.end()};
eval_json(json); else
} m_requests = String{};
catch (runtime_error& error)
{
write_stderr(format("error while executing request '{}': '{}'",
StringView{m_requests.begin(), pos}, error.what()));
}
m_requests = String{pos, m_requests.end()};
} }
if (pos)
m_requests = String{pos, m_requests.end()};
} }
while (not m_pending_keys.empty()) while (m_input_callback and not m_pending_keys.empty())
m_input_callback(mode); m_input_callback(mode);
} }