Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/hoss/faiss/FaissException.cpp
1 /**
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 // -*- c++ -*-
9 
10 #include "FaissException.h"
11 #include <sstream>
12 
13 namespace faiss {
14 
15 FaissException::FaissException(const std::string& m)
16  : msg(m) {
17 }
18 
19 FaissException::FaissException(const std::string& m,
20  const char* funcName,
21  const char* file,
22  int line) {
23  int size = snprintf(nullptr, 0, "Error in %s at %s:%d: %s",
24  funcName, file, line, m.c_str());
25  msg.resize(size + 1);
26  snprintf(&msg[0], msg.size(), "Error in %s at %s:%d: %s",
27  funcName, file, line, m.c_str());
28 }
29 
30 const char*
31 FaissException::what() const noexcept {
32  return msg.c_str();
33 }
34 
36  std::vector<std::pair<int, std::exception_ptr>>& exceptions) {
37  if (exceptions.size() == 1) {
38  // throw the single received exception directly
39  std::rethrow_exception(exceptions.front().second);
40 
41  } else if (exceptions.size() > 1) {
42  // multiple exceptions; aggregate them and return a single exception
43  std::stringstream ss;
44 
45  for (auto& p : exceptions) {
46  try {
47  std::rethrow_exception(p.second);
48  } catch (std::exception& ex) {
49  if (ex.what()) {
50  // exception message available
51  ss << "Exception thrown from index " << p.first << ": "
52  << ex.what() << "\n";
53  } else {
54  // No message available
55  ss << "Unknown exception thrown from index " << p.first << "\n";
56  }
57  } catch (...) {
58  ss << "Unknown exception thrown from index " << p.first << "\n";
59  }
60  }
61 
62  throw FaissException(ss.str());
63  }
64 }
65 
66 }
void handleExceptions(std::vector< std::pair< int, std::exception_ptr >> &exceptions)
const char * what() const noexceptoverride
from std::exception
Base class for Faiss exceptions.