2019-09-27 04:28:25 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
const std::array<http::header, 1> _error_headers
|
|
|
|
{{
|
|
|
|
{ "Content-Type", "application/json; charset=utf-8" },
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_local
|
|
|
|
decltype(ircd::m::error::fmtbuf)
|
|
|
|
ircd::m::error::fmtbuf
|
|
|
|
{};
|
|
|
|
|
|
|
|
//
|
|
|
|
// error::error
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::error::error()
|
|
|
|
:error
|
|
|
|
{
|
|
|
|
internal, http::INTERNAL_SERVER_ERROR, std::string{},
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::error::error(std::string c)
|
|
|
|
:error
|
|
|
|
{
|
|
|
|
internal, http::INTERNAL_SERVER_ERROR, std::move(c),
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::error::error(const http::code &c)
|
|
|
|
:error
|
|
|
|
{
|
|
|
|
internal, c, std::string{},
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::error::error(const http::code &c,
|
|
|
|
const json::members &members)
|
|
|
|
:error
|
|
|
|
{
|
|
|
|
internal, c, json::strung{members},
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::error::error(const http::code &c,
|
|
|
|
const json::iov &iov)
|
|
|
|
:error
|
|
|
|
{
|
|
|
|
internal, c, json::strung{iov},
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::error::error(const http::code &c,
|
|
|
|
const json::object &object)
|
|
|
|
:error
|
|
|
|
{
|
|
|
|
internal, c, json::strung{object},
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::error::error(internal_t,
|
|
|
|
const http::code &c,
|
|
|
|
std::string object)
|
|
|
|
:http::error
|
|
|
|
{
|
|
|
|
c, std::move(object), vector_view<const http::header>{_error_headers}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(!content.empty())
|
|
|
|
{
|
|
|
|
strlcat(ircd::exception::buf, " ");
|
|
|
|
strlcat(ircd::exception::buf, errcode());
|
|
|
|
strlcat(ircd::exception::buf, " :");
|
|
|
|
strlcat(ircd::exception::buf, errstr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 21:12:51 +01:00
|
|
|
// Out-of-line placement
|
|
|
|
ircd::m::error::~error()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::error::errstr()
|
2020-08-05 04:14:58 +02:00
|
|
|
const noexcept try
|
2019-09-27 04:28:25 +02:00
|
|
|
{
|
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
this->http::error::content
|
|
|
|
};
|
|
|
|
|
2020-08-05 04:32:58 +02:00
|
|
|
if(json::type(content, std::nothrow) == json::STRING)
|
|
|
|
return string_view{content};
|
|
|
|
|
2020-08-05 04:14:58 +02:00
|
|
|
const json::string &ret
|
|
|
|
{
|
|
|
|
content["error"]
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return "(There was an error with the error object)"_sv;
|
2019-09-27 04:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::error::errcode()
|
2020-08-05 04:14:58 +02:00
|
|
|
const noexcept try
|
2019-09-27 04:28:25 +02:00
|
|
|
{
|
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
this->http::error::content
|
|
|
|
};
|
|
|
|
|
2020-08-05 04:32:58 +02:00
|
|
|
if(json::type(content, std::nothrow) == json::STRING)
|
|
|
|
return "M_UNKNOWN"_sv;
|
|
|
|
|
2020-08-05 04:14:58 +02:00
|
|
|
const json::string &ret
|
|
|
|
{
|
|
|
|
content.get("errcode", "M_UNKNOWN"_sv)
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return "M_BAD_ERROR"_sv;
|
2019-09-27 04:28:25 +02:00
|
|
|
}
|