11 Errors
Invalid input fails before anything is computed. plot/build (and render, render-image) validates in three rounds, each throwing one ex-info listing all its errors:
- the render options, the plot keys and the merged theme;
- when those hold, every chart’s options and mapping;
- when those hold, the rules across keys (e.g.
:flipneeds the generated grid, not:arrange).
Builders and data functions check their options before reading their data.
(ns reference.errors
(:require [cljplot2.chart :as chart]
[cljplot2.core :as plot]))11.1 Error data
ex-data of the error: :key - the first offending key, :in - its path, :value - the value, :errors - every problem as {:in path :value v :message text}; the exception message lists the same problems as text. A misspelled key gets a spelling suggestion.
(def bad-plot
{:data [{:x 1 :y 2} {:x 2 :y 3}]
:mapping {:x :x :y :y}
:charts [(chart/scatter {:szie 4})]
:scales {:x {:kind :logarithmic}}})(try (plot/build bad-plot)
(catch clojure.lang.ExceptionInfo e {:message (ex-message e) :data (ex-data e)})){:message
"Invalid input: [:scales :x :kind] should be either :bands, :datetime, :interpolated, :linear, :log, :pow, :radial or :symlog, got :logarithmic",
:data
{:key :kind,
:in [:scales :x :kind],
:value :logarithmic,
:errors
[{:in [:scales :x :kind],
:value :logarithmic,
:message
"should be either :bands, :datetime, :interpolated, :linear, :log, :pow, :radial or :symlog"}]}}With the scale fixed, the next round reports the chart’s misspelled option:
(try (plot/build (assoc-in bad-plot [:scales :x :kind] :log))
(catch clojure.lang.ExceptionInfo e (ex-data e))){:key :szie,
:in [:charts 0 :options :szie],
:value 4,
:errors
[{:in [:charts 0 :options :szie],
:value 4,
:message "unknown key, should be spelled :size"}]}Fixed:
(plot/render-image (-> bad-plot
(assoc :charts [(chart/scatter {:size 4})])
(assoc-in [:scales :x :kind] :log))
{:width 500 :height 300})
11.2 Builders and data functions
Options are checked against the builder’s schema before its data is read:
(try (plot/sankey [{:from "a" :to "b" :value 1}] {:node-widht 10})
(catch clojure.lang.ExceptionInfo e (ex-data e))){:key :node-widht,
:in [:node-widht],
:value 10,
:errors
[{:in [:node-widht],
:value 10,
:message "unknown key, should be spelled :node-width"}]}Checks of the data itself (missing columns, wrong shapes) throw the same kind of error, keyed by the option or argument at fault.
11.3 Finding the cause
:until(render options) stops the build after a stage - the built plot is data to inspect.:debug truepaints the layout boxes (Render chapter).
source: notebooks/reference/errors.clj