Fork me on GitHub
#polylith
<
2024-03-18
>
timo10:03:53

Hi, are you guys using exceptions for your components or are you using something different if something goes wrong?

namenu13:03:01

Once we used failjure, now it’s gone and we decided to use ex-info

namenu13:03:47

Really promising alternative to me is missionary. With this approach we can finally make every components “pure”

👍 1
seancorfield15:03:59

My answer is the same regardless of Polylith: • for unexpected errors that local code cannot handle, throw an exception • for "expected" errors / failures that can be handled in local code, return some sort of error code or pair/hash map with either a successful result or details of the failure

👍 2
seancorfield15:03:18

You're on the JVM: lots of code you might call can throw exceptions; they are idiomatic on the JVM.

👍 1
1
jasonjckn23:03:32

we use ex-info with a data that conforms to https://github.com/cognitect-labs/anomalies

jasonjckn23:03:30

if you’re writing higher performance code, and/or have a very large project , i also like https://github.com/redplanetlabs/defexception

👍 1
namenu13:03:59

is there a way to return error with poly info when there is any warnings? I want it to be checked by CI.

👍 1
tengstrand14:03:33

We don’t support that today, but we could e.g. activate it by passing in something like :with-warnings to the info command. You can create an issue if you want.

👍 1
seancorfield15:03:41

You can do it with poly check via the API in 0.2.19. We do it at work in our build.clj file

seancorfield15:03:20

As soon as I get to my desk, I'll share the code

seancorfield15:03:03

This is what we have in our build.clj file:

(defn poly-check
  "Run the Polylith check command."
  [opts]
  (let [{:keys [ok? error-messages]} (poly/check)]
    (when-not ok?
      (doseq [{:keys [code colorized-message type]} error-messages]
        (println colorized-message ":" type code))
      (throw (ex-info "Polylith Check Failed!" {}))))
  opts)
so clojure -T:build poly-check will fail the build if there are errors/warnings.

❤️ 3
seancorfield15:03:38

We :require [polylith.clj.core.api.interface :as poly] in the build ns.

namenu00:03:13

Thanks! I’ll copy and paste that code right away

😁 1
namenu05:04:22

poly/check doesn't catch warnings so I ended up like this.

- name: Check poly info
  run: |
    output=$(clojure -M:poly info color-mode:none)
    if echo "$output" | grep -q "Warning"; then
      warnings=$(echo "$output" | sed -n '/Warning/,$p')
      while IFS= read -r line; do
        echo "::warning::$line"
      done <<< "$warnings"
      exit 1
    fi

tengstrand06:04:37

An option would be to make this configurable.

seancorfield06:04:39

I'm a bit surprised that poly/check doesn't return warnings in that data structure -- guess I hadn't noticed (and failing on errors has been sufficient so far for us)?

tengstrand06:04:42

Do you mean it doesn’t populate :messages or that it doesn’t return a code (e.g. 1)?

seancorfield06:04:21

It returns a hash map with :ok? and :error-messages etc.

seancorfield06:04:04

See the poly-check function I posted above, from our build.clj file at work.

tengstrand06:04:43

Okay, then I think we understand each other! This could of course be configurable with something like treat-warning-as-error and then if we have a warning when running the test command, it could e.g. return with 1.

seancorfield06:04:01

I don't think we do understand each other. In the API does the check function return warnings or just errors? Is :ok? still truthy in the presence of warnings? I'm not asking about anything else except that API function.

namenu06:04:35

In the API does the check function return warnings or just errors? => just errors Is :ok? still truthy in the presence of warnings? => yes -- The validator that poly/check depends on only provides an interface for errors I guess. https://github.com/polyfy/polylith/blob/338b36f724c899e4fe994eb9d3dae9a37f04bd46/components/validator/src/polylith/clj/core/validator/core.clj

tengstrand06:04:04

Okay, get it now. We only return errors right now. One solution could be to also include the warnings. The :ok? flag could work as today and be set if there are any errors.

seancorfield06:04:45

That would be nice. So callers could check whether any warnings came back when :ok? is truthy. I'd like that.

tengstrand06:04:45

Yes, that's my idea. We could add :has-warnings? or similar too.