Commit 0c8b9b46 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

improved error handling

parent 00994f14
Loading
Loading
Loading
Loading
+9 −9
Original line number Diff line number Diff line
@@ -147,7 +147,7 @@ ReturnCode configure_float(
  try {
    *value = std::stod(prop);
  } catch (... ) {
    return ERROR_INVALID_ARGUMENT;
    return ERROR;
  }

  return OK;
@@ -166,7 +166,7 @@ ReturnCode configure_float_opt(
  try {
    *value = std::optional<double>(std::stod(prop));
  } catch (... ) {
    return ERROR_INVALID_ARGUMENT;
    return ERROR;
  }

  return OK;
@@ -253,11 +253,11 @@ ReturnCode parse_datasource_csv(
    const plist::Property& prop ,
    DataContext* ctx) {
  if (!plist::is_enum(prop, "csv")) {
    return ERROR_INVALID_ARGUMENT;
    return ERROR;
  }

  if (prop.size() < 1) {
    return ERROR_INVALID_ARGUMENT; // FIXME
    return ERROR; // FIXME
  }

  std::string csv_path;
@@ -284,7 +284,7 @@ ReturnCode configure_datasource_prop(
    return parse_datasource_csv(prop, data);
  }

  return ERROR_INVALID_ARGUMENT;
  return ERROR;
}

ReturnCode configure_datasource(
@@ -315,7 +315,7 @@ ReturnCode parse_data_series_csv(
    const plist::Property& prop,
    SeriesRef* data_ref) {
  if (!plist::is_enum(prop, "csv")) {
    return ERROR_INVALID_ARGUMENT;
    return ERROR;
  }

  if (prop.size() < 2) {
@@ -355,7 +355,7 @@ ReturnCode parse_data_series_inline(
    const plist::Property& prop,
    SeriesRef* data_ref) {
  if (!plist::is_enum(prop, "inline")) {
    return ERROR_INVALID_ARGUMENT;
    return ERROR;
  }

  auto data = std::make_shared<Series>();
@@ -372,7 +372,7 @@ ReturnCode parse_data_series_var(
    const DataContext& ctx,
    SeriesRef* data) {
  if (!plist::is_value_literal(prop)) {
    return ERROR_INVALID_ARGUMENT;
    return ERROR;
  }

  const auto& var_name = prop.value;
@@ -401,7 +401,7 @@ ReturnCode configure_series(
    return parse_data_series_var(prop, ctx, data);
  }

  return ERROR_INVALID_ARGUMENT;
  return ERROR;
}

ParserFn configure_series_fn(
+8 −0
Original line number Diff line number Diff line
@@ -218,5 +218,13 @@ ReturnCode document_render_png(
  return OK;
}

void ctx_seterrf(plotfx_t* ctx, const std::string& err) {
  static_cast<Context*>(ctx)->error = err;
}

void ctx_seterr(plotfx_t* ctx, const ReturnCode& err) {
  static_cast<Context*>(ctx)->error = err.getMessage();
}

} // namespace plotfx
+13 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#pragma once
#include <memory>
#include "utils/return_code.h"
#include "source/color_scheme.h"
#include "source/data_model.h"
@@ -39,6 +40,11 @@
namespace plotfx {
class Layer;

struct Context {
  std::unique_ptr<Document> document;
  mutable std::string error;
};

struct Document {
  Document();
  Measure width;
@@ -69,6 +75,10 @@ ReturnCode document_render(
    const std::string& format,
    const std::string& filename);

ReturnCode document_render_to(
    const Document& tree,
    Layer* layer);

ReturnCode document_render_svg(
    const Document& doc,
    const std::string& filename);
@@ -77,5 +87,8 @@ ReturnCode document_render_png(
    const Document& doc,
    const std::string& filename);

void ctx_seterrf(plotfx_t* ctx, const std::string& err);
void ctx_seterr(plotfx_t* ctx, const ReturnCode& err);

} // namespace plotfx
+1 −1
Original line number Diff line number Diff line
@@ -300,7 +300,7 @@ ReturnCode domain_configure(
      continue;
    }

    return ERROR_INVALID_ARGUMENT;
    return ERROR;
  }

  return OK;
+7 −7
Original line number Diff line number Diff line
@@ -55,10 +55,10 @@ ReturnCode confgure_format_decimal_scientific(
        precision = std::stod(prop[0]);
        break;
      } catch (... ) {
        return ERROR_INVALID_ARGUMENT;
        return ERROR;
      }
    default:
      return ERROR_INVALID_ARGUMENT;
      return ERROR;
  }

  *formatter = format_decimal_scientific(precision);
@@ -77,7 +77,7 @@ ReturnCode confgure_format_decimal_fixed(
    const plist::Property& prop,
    Formatter* formatter) {
  if (!plist::is_enum(prop, "fixed")) {
    return ERROR_INVALID_ARGUMENT;
    return ERROR;
  }

  uint32_t precision = 1;
@@ -89,10 +89,10 @@ ReturnCode confgure_format_decimal_fixed(
        precision = std::stod(prop[0]);
        break;
      } catch (... ) {
        return ERROR_INVALID_ARGUMENT;
        return ERROR;
      }
    default:
      return ERROR_INVALID_ARGUMENT;
      return ERROR;
  }

  *formatter = format_decimal_fixed(precision);
@@ -110,7 +110,7 @@ ReturnCode confgure_format_datetime(
    const plist::Property& prop,
    Formatter* formatter) {
  if (!plist::is_enum(prop, "datetime")) {
    return ERROR_INVALID_ARGUMENT;
    return ERROR;
  }

  std::string fmtspec = "%Y-%m-%d %H:%M:%S"; // FIXME improved auto format
@@ -121,7 +121,7 @@ ReturnCode confgure_format_datetime(
      fmtspec = prop[0];
      break;
    default:
      return ERROR_INVALID_ARGUMENT;
      return ERROR;
  }

  *formatter = format_datetime(fmtspec);
Loading