Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1380 function clause error changeset json error #1393

Merged
merged 7 commits into from
Nov 20, 2023

Conversation

benedictus-dev
Copy link
Contributor

@benedictus-dev benedictus-dev commented Nov 15, 2023

Notes for the reviewer

Update the CompleteRun handler in Lightning.Attempts.Handlers to handle
cases where a run is not found by returning a detailed error changeset.

Related issue

Fixes #1380

Review checklist

  • I have performed a self-review of my code
  • I have verified that all appropriate authorization policies have been implemented and tested
  • If needed, I have updated the changelog
  • Product has QA'd this feature

Copy link

codecov bot commented Nov 15, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (448a320) 85.82% compared to head (e4de1a3) 85.81%.
Report is 9 commits behind head on main.

Files Patch % Lines
lib/lightning/attempts/handlers.ex 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1393      +/-   ##
==========================================
- Coverage   85.82%   85.81%   -0.02%     
==========================================
  Files         216      216              
  Lines        6393     6393              
==========================================
- Hits         5487     5486       -1     
- Misses        906      907       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 172 to 178
case get_run(complete_run.run_id) do
nil ->
# Return an error changeset when the run is not found
%Run{}
|> change()
|> put_change(:exit_reason, "error")
|> put_change(:error_type, "Run not found")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is the way to go about it. In the case of an error we don't return a Run changeset, we return a CompleteRun changeset, it's purpose is to stand in between the worker sending a bunch of data that represents data that is for a run and a dataclip. It can be thought of as a 'command'.

My suggestion is to add a run: Run field to the schema, we don't cast it - but set the field and then validate it.

  # ... ^ new/1
  |> then(&assign_run/1)
end

defp assign_run(changeset) do
  with %{valid?: true} <- changeset,
        %Run{} = run <-
          get_field(changeset, :run_id) |> get_run() || :not_found do
    put_change(changeset, :run, run)
  else
    :not_found ->
      add_error(changeset, :run_id, "not found")

    changeset ->
      changeset
  end
end

And then further down the update/1 function can assume the changeset is valid, and get the run off the changeset instead of finding it itself.

Copy link
Contributor Author

@benedictus-dev benedictus-dev Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposal is to be very granular with the fix changing only the flow of the complete run.
The change consists of just adding the changeset to the returned value.

Copy link
Contributor

@jyeshe jyeshe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to keep the previous order of validations and I think this change as 9ef9359 is safer, going directly to the point by adding the validation only to the flow reported on the error (on new/insertion the validation won't be needed)

Comment on lines 170 to 171
with {:ok, _} <- to_dataclip(complete_run) |> Repo.insert(),
%Run{} = run <- get_run(complete_run.run_id) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry Ben, just noticed after the pairing that the get_run makes more sense before the dataclip insertion as it was previously.

Comment on lines 174 to 178
nil ->
{:error,
%Run{}
|> change()
|> add_error(:run_id, "not found")}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't return a Run changeset. As mentioned in my first review:

In the case of an error we don't return a Run changeset, we return a CompleteRun changeset

The Run model does not have a :run_id key. You have access to the original CompleteRun changeset. Please change the nil case to:

{:error, complete_run |> add_error(:run_id, "not found")}

@stuartc stuartc merged commit 73bfa71 into main Nov 20, 2023
6 of 8 checks passed
@stuartc stuartc deleted the 1380-function-clause-error-changeset-json-error branch November 20, 2023 10:12
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FunctionClauseError: no function clause matching in LightningWeb.ChangesetJSON.error/1
4 participants