AF
Ash Framework•4mo ago
Ahrou

Is there any public function to prettify a BulkResult/Change error?

I want to display a :string error message but in this case there is none, i think AshJsonApi translates this errors to a message somehow, is that function public?
%Ash.BulkResult{
status: :partial_success,
errors: [
%Ash.Error.Invalid{
bread_crumbs: ["Exception raised in bulk create: App.Core.Contact.create"],
changeset: "#Changeset<>",
errors: [
%Ash.Error.Changes.Required{
field: :language,
type: :attribute,
resource: App.Core.Contact,
splode: Ash.Error,
bread_crumbs: ["Exception raised in bulk create: App.Core.Contact.create"],
vars: [],
path: [],
stacktrace: #Splode.Stacktrace<>,
class: :invalid
}
]
}
],
records: nil,
notifications: nil,
error_count: 1
}
%Ash.BulkResult{
status: :partial_success,
errors: [
%Ash.Error.Invalid{
bread_crumbs: ["Exception raised in bulk create: App.Core.Contact.create"],
changeset: "#Changeset<>",
errors: [
%Ash.Error.Changes.Required{
field: :language,
type: :attribute,
resource: App.Core.Contact,
splode: Ash.Error,
bread_crumbs: ["Exception raised in bulk create: App.Core.Contact.create"],
vars: [],
path: [],
stacktrace: #Splode.Stacktrace<>,
class: :invalid
}
]
}
],
records: nil,
notifications: nil,
error_count: 1
}
Solution:
so like AshJsonApi.Error.to_json_api_errors(Domain, Resource, error, :create)
Jump to solution
6 Replies
ZachDaniel
ZachDaniel•4mo ago
domain
|> AshJsonApi.Error.to_json_api_errors(resource, error, action.type)
domain
|> AshJsonApi.Error.to_json_api_errors(resource, error, action.type)
Youu can pass the errors into that to get normalized errors back
Ahrou
AhrouOP•4mo ago
do i need the resource struct or the Module is enough?
AshJsonApi.Error.to_json_api_errors(Core, Core.Contact, error, :create)
AshJsonApi.Error.to_json_api_errors(Core, Core.Contact, error, :create)
Because since this is a bulk create i dont have any resource, but with the code above i get this error:
** (ErlangError) Erlang error: %Ash.BulkResult{status: :error, errors: [%Ash.Error.Invalid{changeset: "#Changeset<>", errors: [%Ash.Error.Changes.Required{field: :language, type: :attribute, resource: App.Core.Contact, splode: Ash.Error, bread_crumbs: [], vars: [], path: [], stacktrace: #Splode.Stacktrace<>, class: :invalid}]}], records: nil, notifications: nil, error_count: 1}
(ash_json_api 1.4.36) lib/ash_json_api/error/error.ex:109: AshJsonApi.Error.to_json_api_errors/4
** (ErlangError) Erlang error: %Ash.BulkResult{status: :error, errors: [%Ash.Error.Invalid{changeset: "#Changeset<>", errors: [%Ash.Error.Changes.Required{field: :language, type: :attribute, resource: App.Core.Contact, splode: Ash.Error, bread_crumbs: [], vars: [], path: [], stacktrace: #Splode.Stacktrace<>, class: :invalid}]}], records: nil, notifications: nil, error_count: 1}
(ash_json_api 1.4.36) lib/ash_json_api/error/error.ex:109: AshJsonApi.Error.to_json_api_errors/4
ZachDaniel
ZachDaniel•4mo ago
You should pass in the .errors and the resource is the module not an instance
Solution
ZachDaniel
ZachDaniel•4mo ago
so like AshJsonApi.Error.to_json_api_errors(Domain, Resource, error, :create)
ZachDaniel
ZachDaniel•4mo ago
you may need to loop over the errors...not sure you will have to experiment
Ahrou
AhrouOP•4mo ago
that was the issue, just needed .errors thank you 🙌
AshJsonApi.Error.to_json_api_errors(Core, Core.Contact, error.errors, :create) #=> [
%AshJsonApi.Error{
id: "d1d5ddd6-273c-4c7f-9459-10f95865ed9f",
about: :undefined,
code: "required",
title: "Required",
detail: "is required",
source_pointer: "/data/attributes/language",
source_parameter: :undefined,
meta: %{},
status_code: 400,
internal_description: nil,
log_level: :debug
}
]
AshJsonApi.Error.to_json_api_errors(Core, Core.Contact, error.errors, :create) #=> [
%AshJsonApi.Error{
id: "d1d5ddd6-273c-4c7f-9459-10f95865ed9f",
about: :undefined,
code: "required",
title: "Required",
detail: "is required",
source_pointer: "/data/attributes/language",
source_parameter: :undefined,
meta: %{},
status_code: 400,
internal_description: nil,
log_level: :debug
}
]

Did you find this page helpful?