Commit 3bf4457d authored by Paul Asmuth's avatar Paul Asmuth
Browse files

RUNTIME_EXCEPTION_ERRNO

parent 67764478
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -16,8 +16,9 @@ std::unique_ptr<CSVFile> CSVFile::openFile(const std::string& file_path) {
  int fd = open(file_path.c_str(), O_RDONLY);

  if (fd < 1) {
    throw RUNTIME_EXCEPTION(
      "error opening file '%s':",
    throw RUNTIME_EXCEPTION_ERRNO(
        ERR_CANNOT_OPEN_FILE,
        "error opening file '%s'",
        file_path.c_str());
  }

+4 −0
Original line number Diff line number Diff line
@@ -17,6 +17,10 @@ class CSVFile {
  friend class CSVBackendTest;
public:

  enum ErrorCodes {
    ERR_CANNOT_OPEN_FILE = 1000
  };

  /**
   * Open a new csv file from the provided file path. Throws an exception if
   * the file cannot be opened.
+21 −3
Original line number Diff line number Diff line
@@ -6,32 +6,50 @@
 */
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "runtimeexception.h"

namespace fnordmetric {
namespace util {

RuntimeException::RuntimeException(
    int type_id,
    const char* type_human,
    const char* file,
    int line,
    const char* func,
    int posix_errno,
    const char* message,
    ...) :
    type_id_(type_id),
    type_human_(type_human),
    file_(file),
    line_(line),
    func_(func) {
  va_list args;
  va_start(args, args);
  vsnprintf(message_, sizeof(message_), message, args);
  va_start(args, message);
  int pos = vsnprintf(message_, sizeof(message_), message, args);
  va_end(args);

  if (pos < 0) {
    pos = 0;
  }

  if (errno > 0) {
    snprintf(message_ + pos, sizeof(message_) - pos, ": ");
    pos += 2;
    strerror_r(errno, message_ + pos, sizeof(message_) - pos);
  }
}

void RuntimeException::debugPrint() const {
  fprintf(
      stderr,
      "RuntimeException[1234]: %s\n"
      "RuntimeException[%s] (%i): %s\n"
      "    in %s\n"
      "    in %s:%i\n",
      type_human_,
      type_id_,
      message_,
      func_,
      file_,
+21 −5
Original line number Diff line number Diff line
@@ -6,14 +6,25 @@
 */
#ifndef _FNORDMETRIC_UTIL_RUNTIMEXCEPTION_H
#define _FNORDMETRIC_UTIL_RUNTIMEXCEPTION_H
#include <errno.h>
#include <exception>

#define RUNTIME_EXCEPTION(...) fnordmetric::util::RuntimeException( \
#define __RUNTIME_EXCEPTION(T, E, ...) \
    fnordmetric::util::RuntimeException( \
        T, \
        #T, \
        __FILE__, \
        __LINE__, \
        __PRETTY_FUNCTION__, \
        E, \
        __VA_ARGS__)

#define RUNTIME_EXCEPTION(T, ...) \
    __RUNTIME_EXCEPTION(T, -1, __VA_ARGS__)

#define RUNTIME_EXCEPTION_ERRNO(T, ...) \
    __RUNTIME_EXCEPTION(T, errno, __VA_ARGS__)

namespace fnordmetric {
namespace util {

@@ -21,15 +32,20 @@ class RuntimeException : public std::exception {
public:

  RuntimeException(
      int type_id,
      const char* type_human,
      const char* file,
      int line,
      const char* func,
      int posix_errno,
      const char* message,
      ...);

  void debugPrint() const;

private:
  int type_id_;
  const char* type_human_;
  const char* file_;
  int line_;
  const char* func_;