Leosac  0.8.0
Open Source Access Control
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 }
LEOSACException::what
virtual const char * what() const noexcept final
Definition: leosacexception.hpp:53
Leosac::APIStatusCode::MODEL_EXCEPTION
@ MODEL_EXCEPTION
Some internal API rules has failed.
LEOSACException::trace
const Leosac::Tools::Stacktrace & trace() const
Get the stacktrace associated with this exception.
Definition: leosacexception.hpp:61
WARN
@ WARN
Definition: log.hpp:33
Exceptions.hpp
BUILD_STR
#define BUILD_STR(param)
Internal macro.
Definition: log.hpp:63
Leosac::Module::WebSockAPI::ServerMessage::status_code
APIStatusCode status_code
Definition: Messages.hpp:44
ERROR
@ ERROR
Definition: log.hpp:32
Leosac::Module::WebSockAPI::ServerMessage
A message sent by the server to a client.
Definition: Messages.hpp:38
Leosac::APIStatusCode::INVALID_ARGUMENT
@ INVALID_ARGUMENT
One of the argument of the call had a invalid value / type.
Leosac::APIStatusCode::ENTITY_NOT_FOUND
@ ENTITY_NOT_FOUND
The requested entity cannot be found.
Leosac::Module::WebSockAPI::ServerMessage::uuid
std::string uuid
Definition: Messages.hpp:46
Leosac::APIStatusCode::SESSION_ABORTED
@ SESSION_ABORTED
The session has been aborted.
Leosac::Module
All modules that provides features to Leosac shall be in this namespace.
Leosac::EntityNotFound
Definition: EntityNotFound.hpp:27
Leosac::Module::WebSockAPI::MalformedMessage
Definition: Exceptions.hpp:32
Leosac::Module::WebSockAPI::ExceptionConverter::convert_merge
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.
Definition: ExceptionConverter.cpp:33
Leosac::Tools::Stacktrace::str
std::string str(int max_frames=10) const
Definition: Stacktrace.cpp:68
Leosac::APIStatusCode::GENERAL_FAILURE
@ GENERAL_FAILURE
A failure for an unknown reason.
Leosac::Module::WebSockAPI::InvalidCall
Definition: Exceptions.hpp:41
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
PermissionDenied.hpp
Leosac::Module::WebSockAPI::ServerMessage::type
std::string type
Definition: Messages.hpp:47
Leosac::Module::WebSockAPI::ServerMessage::status_string
std::string status_string
Definition: Messages.hpp:45
ExceptionConverter.hpp
Leosac::APIStatusCode::PERMISSION_DENIED
@ PERMISSION_DENIED
The websocket connection is not allowed to make the requested API call.
LEOSACException
A base class for Leosac specific exception.
Definition: leosacexception.hpp:40
Leosac::InvalidArgument
A class to represents invalid argument exception in Leosac.
Definition: InvalidArgument.hpp:41
ModelException::json_errors
json json_errors() const
Format the ModelError object(s).
Definition: ModelException.cpp:43
Leosac::APIStatusCode::MALFORMED
@ MALFORMED
The source packet was malformed.
ModelException.hpp
Leosac::Module::WebSockAPI::ExceptionConverter::convert_impl
virtual ServerMessage convert_impl(const std::exception_ptr &ptr)
Definition: ExceptionConverter.cpp:61
Leosac::Module::WebSockAPI::ServerMessage::content
json content
Definition: Messages.hpp:48
Leosac::Module::WebSockAPI::SessionAborted
Definition: Exceptions.hpp:48
Leosac::EntityNotFound::entity_type
const std::string & entity_type() const
Definition: EntityNotFound.cpp:52
ModelException
An exception class for general API error.
Definition: ModelException.hpp:33
Leosac::APIStatusCode::INVALID_CALL
@ INVALID_CALL
The API method (ie, message's type) does not exist.
log.hpp
Leosac::Module::WebSockAPI
Definition: ActionActionParam.hpp:28
InvalidArgument.hpp
Leosac::APIStatusCode::DATABASE_ERROR
@ DATABASE_ERROR
An internal database operation threw an exception.
EntityNotFound.hpp
Leosac::EntityNotFound::entity_id
const std::string & entity_id() const
Definition: EntityNotFound.cpp:47
PermissionDenied
An exception that can be throw when the permission for a given operation is denied.
Definition: PermissionDenied.hpp:28