All files / src/pages/flowsheet-page/economics/settings-panel/model settingsApiErrors.ts

0% Statements 0/21
0% Branches 0/17
0% Functions 0/3
0% Lines 0/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88                                                                                                                                                                               
import type { FieldMessages } from "./settingsTypes";
 
export function apiFieldMessages<TDraft>(
  error: unknown,
  fieldKeys: readonly (keyof TDraft)[],
): FieldMessages<TDraft> {
  if (!error || typeof error !== "object" || !("data" in error)) {
    return {};
  }
  const data = error.data;
  if (!data || typeof data !== "object" || Array.isArray(data)) {
    return {};
  }
  const entries = data as Partial<Record<keyof TDraft, unknown>>;
  const messages: FieldMessages<TDraft> = {};
  for (const field of fieldKeys) {
    if (field in entries) {
      messages[field] = flattenErrorData(entries[field]);
    }
  }
  return messages;
}
 
export function errorMessage(error: unknown) {
  if (!error || typeof error !== "object") {
    return "The economics configuration could not be saved.";
  }
  if ("data" in error) {
    return flattenErrorData(error.data, economicsFieldLabel);
  }
  if ("message" in error && typeof error.message === "string") {
    return error.message;
  }
  return "The economics configuration could not be saved.";
}
 
function flattenErrorData(
  data: unknown,
  labelForField: (field: string) => string = (field) => field,
): string {
  if (typeof data === "string") {
    return looksLikeHtmlDocument(data)
      ? "The economics configuration could not be saved."
      : data;
  }
  if (Array.isArray(data)) {
    return data.map((item) => flattenErrorData(item, labelForField)).join(" ");
  }
  if (data && typeof data === "object") {
    return Object.entries(data)
      .map(
        ([field, value]) =>
          `${labelForField(field)}: ${flattenErrorData(value, labelForField)}`,
      )
      .join(" ");
  }
  return "The economics configuration could not be saved.";
}
 
function looksLikeHtmlDocument(value: string) {
  return /^\s*(?:<!doctype\s+html|<html[\s>])/i.test(value);
}
 
function economicsFieldLabel(field: string) {
  const labels: Record<string, string> = {
    manual_capex: "Baseline capex",
    manual_annual_opex: "Annual opex",
    annual_heat_basis_mode: "Annual heat quantity",
    manual_annual_heat_basis: "Annual heat quantity",
    manual_annual_heat_basis_unit: "Annual heat quantity unit",
    average_power_input: "Hourly heat quantity",
    average_power_unit: "Annual heat quantity unit",
    electrical_upgrade_rate_amount: "Electrical upgrade rate",
    default_lang_factor: "Default Lang factor",
    tax_rate_percent: "Corporate tax rate",
    depreciation_enabled: "Straight-line depreciation",
    default_depreciation_life_years: "Default equipment life",
    default_depreciation_salvage_percent: "Default residual value",
    default_rate_overrides: "Utility and maintenance defaults",
    discount_rate_percent: "Discount rate",
    project_lifetime_years: "Project lifetime",
    annual_operating_hours: "Annual operating hours",
    capital_index_series: "Capital index series",
    operating_index_series: "Operating index series",
  };
  return labels[field] ?? field.replace(/_/g, " ");
}