Leosac  0.7.0
OpenSourceAccessControl
ExceptionConverter.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "ExceptionConverter.hpp"
21 #include "Exceptions.hpp"
26 #include "tools/log.hpp"
27 #include <boost/type_index.hpp>
28 
29 using namespace Leosac;
30 using namespace Leosac::Module;
31 using namespace Leosac::Module::WebSockAPI;
32 
33 ServerMessage ExceptionConverter::convert_merge(const std::exception_ptr &ptr,
34  const ServerMessage &msg)
35 {
36  auto converted_msg = convert_impl(ptr);
37  converted_msg.uuid = msg.uuid;
38  converted_msg.type = msg.type;
39 
40  return converted_msg;
41 }
42 
46 static std::string exception_name(const std::exception_ptr &ptr)
47 {
48  try
49  {
50  if (ptr)
51  std::rethrow_exception(ptr);
52  return "";
53  }
54  catch (const std::exception &e)
55  {
56  auto type_index = boost::typeindex::type_id_runtime(e);
57  return type_index.pretty_name();
58  }
59 }
60 
61 ServerMessage ExceptionConverter::convert_impl(const std::exception_ptr &ptr)
62 {
63  ServerMessage response;
64  std::string ename = exception_name(ptr);
65  try
66  {
67  if (ptr)
68  std::rethrow_exception(ptr);
69  return response;
70  }
71  catch (const InvalidCall &e)
72  {
74  response.status_string = e.what();
75  }
76  catch (const PermissionDenied &e)
77  {
79  response.status_string = e.what();
80  }
81  catch (const MalformedMessage &e)
82  {
83  WARN("ST: " << e.trace().str());
85  response.status_string = e.what();
86  }
87  catch (const SessionAborted &e)
88  {
90  response.status_string = e.what();
91  }
92  catch (const EntityNotFound &e)
93  {
95  response.status_string = e.what();
96  response.content["entity_id"] = e.entity_id();
97  response.content["entity_type"] = e.entity_type();
98  }
99  catch (const ModelException &e)
100  {
102  response.status_string = e.what();
103  response.content["errors"] = e.json_errors();
104  }
105  catch (const InvalidArgument &e)
106  {
108  response.status_string = e.what();
109  }
110  catch (const LEOSACException &e)
111  {
112  WARN("Leosac specific exception has been caught: " << e.what() << std::endl
113  << e.trace().str());
115  response.status_string = e.what(); // todo Maybe remove in production.
116  }
117  catch (const odb::exception &e)
118  {
119  ERROR("Database Error: " << e.what());
121  response.status_string = "Database Error: " + std::string(e.what());
122  }
123  catch (const std::exception &e)
124  {
125  WARN("Exception when processing request: " << e.what());
127  response.status_string = e.what();
128  }
129  // If we're here, we had an exception. Prepend the status_string
130  // with the type of the exception.
131  response.status_string =
132  BUILD_STR("[" << ename << "]: " << response.status_string);
133  return response;
134 }
The session has been aborted.
json json_errors() const
Format the ModelError object(s).
An internal database operation threw an exception.
An exception class for general API error.
virtual ServerMessage convert_impl(const std::exception_ptr &ptr)
This is the header file for a generated source file, GitSHA1.cpp.
std::string str(int max_frames=10) const
Definition: Stacktrace.cpp:68
One of the argument of the call had a invalid value / type.
Definition: log.hpp:35
The API method (ie, message&#39;s type) does not exist.
virtual const char * what() const noexcept final
An exception that can be throw when the permission for a given operation is denied.
const std::string & entity_type() const
All modules that provides features to Leosac shall be in this namespace.
The requested entity cannot be found.
A class to represents invalid argument exception in Leosac.
The source packet was malformed.
The websocket connection is not allowed to make the requested API call.
const std::string & entity_id() const
Definition: log.hpp:34
#define BUILD_STR(param)
Internal macro.
Definition: log.hpp:66
A base class for Leosac specific exception.
A message sent by the server to a client.
Definition: Messages.hpp:38
const Leosac::Tools::Stacktrace & trace() const
Get the stacktrace associated with this exception.
Some internal API rules has failed.
A failure for an unknown reason.
ServerMessage convert_merge(const std::exception_ptr &ptr, const ServerMessage &msg)
Convert the exception_ptr to a ServerMessage and merge it with an other message.