1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ApiSpec.RenderError do
8 import Plug.Conn, only: [put_status: 2]
9 import Phoenix.Controller, only: [json: 2]
10 import Pleroma.Web.Gettext
13 def init(opts), do: opts
17 def call(conn, errors) do
20 %{name: nil, reason: :invalid_enum} = err ->
21 %OpenApiSpex.Cast.Error{err | name: err.value}
24 %OpenApiSpex.Cast.Error{err | name: List.last(err.path)}
31 |> put_status(:bad_request)
33 error: errors |> Enum.map(&message/1) |> Enum.join(" "),
34 errors: errors |> Enum.map(&render_error/1)
38 defp render_error(error) do
39 pointer = OpenApiSpex.path_to_string(error)
42 title: "Invalid value",
46 message: OpenApiSpex.Cast.Error.message(error)
50 defp message(%{reason: :invalid_schema_type, type: type, name: name}) do
51 gettext("%{name} - Invalid schema.type. Got: %{type}.",
57 defp message(%{reason: :null_value, name: name} = error) do
60 gettext("%{name} - null value.", name: name)
63 gettext("%{name} - null value where %{type} expected.",
70 defp message(%{reason: :all_of, meta: %{invalid_schema: invalid_schema}}) do
72 "Failed to cast value as %{invalid_schema}. Value must be castable using `allOf` schemas listed.",
73 invalid_schema: invalid_schema
77 defp message(%{reason: :any_of, meta: %{failed_schemas: failed_schemas}}) do
78 gettext("Failed to cast value using any of: %{failed_schemas}.",
79 failed_schemas: failed_schemas
83 defp message(%{reason: :one_of, meta: %{failed_schemas: failed_schemas}}) do
84 gettext("Failed to cast value to one of: %{failed_schemas}.", failed_schemas: failed_schemas)
87 defp message(%{reason: :min_length, length: length, name: name}) do
88 gettext("%{name} - String length is smaller than minLength: %{length}.",
94 defp message(%{reason: :max_length, length: length, name: name}) do
95 gettext("%{name} - String length is larger than maxLength: %{length}.",
101 defp message(%{reason: :unique_items, name: name}) do
102 gettext("%{name} - Array items must be unique.", name: name)
105 defp message(%{reason: :min_items, length: min, value: array, name: name}) do
106 gettext("%{name} - Array length %{length} is smaller than minItems: %{min}.",
108 length: length(array),
113 defp message(%{reason: :max_items, length: max, value: array, name: name}) do
114 gettext("%{name} - Array length %{length} is larger than maxItems: %{}.",
116 length: length(array),
121 defp message(%{reason: :multiple_of, length: multiple, value: count, name: name}) do
122 gettext("%{name} - %{count} is not a multiple of %{multiple}.",
129 defp message(%{reason: :exclusive_max, length: max, value: value, name: name})
131 gettext("%{name} - %{value} is larger than exclusive maximum %{max}.",
138 defp message(%{reason: :maximum, length: max, value: value, name: name})
140 gettext("%{name} - %{value} is larger than inclusive maximum %{max}.",
147 defp message(%{reason: :exclusive_multiple, length: min, value: value, name: name})
149 gettext("%{name} - %{value} is smaller than exclusive minimum %{min}.",
156 defp message(%{reason: :minimum, length: min, value: value, name: name})
158 gettext("%{name} - %{value} is smaller than inclusive minimum %{min}.",
165 defp message(%{reason: :invalid_type, type: type, value: value, name: name}) do
166 gettext("%{name} - Invalid %{type}. Got: %{value}.",
168 value: OpenApiSpex.TermType.type(value),
173 defp message(%{reason: :invalid_format, format: format, name: name}) do
174 gettext("%{name} - Invalid format. Expected %{format}.", name: name, format: inspect(format))
177 defp message(%{reason: :invalid_enum, name: name}) do
178 gettext("%{name} - Invalid value for enum.", name: name)
181 defp message(%{reason: :polymorphic_failed, type: polymorphic_type}) do
182 gettext("Failed to cast to any schema in %{polymorphic_type}",
183 polymorphic_type: polymorphic_type
187 defp message(%{reason: :unexpected_field, name: name}) do
188 gettext("Unexpected field: %{name}.", name: safe_string(name))
191 defp message(%{reason: :no_value_for_discriminator, name: field}) do
192 gettext("Value used as discriminator for `%{field}` matches no schemas.", name: field)
195 defp message(%{reason: :invalid_discriminator_value, name: field}) do
196 gettext("No value provided for required discriminator `%{field}`.", name: field)
199 defp message(%{reason: :unknown_schema, name: name}) do
200 gettext("Unknown schema: %{name}.", name: name)
203 defp message(%{reason: :missing_field, name: name}) do
204 gettext("Missing field: %{name}.", name: name)
207 defp message(%{reason: :missing_header, name: name}) do
208 gettext("Missing header: %{name}.", name: name)
211 defp message(%{reason: :invalid_header, name: name}) do
212 gettext("Invalid value for header: %{name}.", name: name)
215 defp message(%{reason: :max_properties, meta: meta}) do
217 "Object property count %{property_count} is greater than maxProperties: %{max_properties}.",
218 property_count: meta.property_count,
219 max_properties: meta.max_properties
223 defp message(%{reason: :min_properties, meta: meta}) do
225 "Object property count %{property_count} is less than minProperties: %{min_properties}",
226 property_count: meta.property_count,
227 min_properties: meta.min_properties
231 defp safe_string(string) do
232 to_string(string) |> String.slice(0..39)