1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
21 %OpenApiSpex.Cast.Error{err | name: List.last(err.path)}
28 |> put_status(:bad_request)
30 error: errors |> Enum.map(&message/1) |> Enum.join(" "),
31 errors: errors |> Enum.map(&render_error/1)
35 defp render_error(error) do
36 pointer = OpenApiSpex.path_to_string(error)
39 title: "Invalid value",
43 message: OpenApiSpex.Cast.Error.message(error)
47 defp message(%{reason: :invalid_schema_type, type: type, name: name}) do
48 gettext("%{name} - Invalid schema.type. Got: %{type}.",
54 defp message(%{reason: :null_value, name: name} = error) do
57 gettext("%{name} - null value.", name: name)
60 gettext("%{name} - null value where %{type} expected.",
67 defp message(%{reason: :all_of, meta: %{invalid_schema: invalid_schema}}) do
69 "Failed to cast value as %{invalid_schema}. Value must be castable using `allOf` schemas listed.",
70 invalid_schema: invalid_schema
74 defp message(%{reason: :any_of, meta: %{failed_schemas: failed_schemas}}) do
75 gettext("Failed to cast value using any of: %{failed_schemas}.",
76 failed_schemas: failed_schemas
80 defp message(%{reason: :one_of, meta: %{failed_schemas: failed_schemas}}) do
81 gettext("Failed to cast value to one of: %{failed_schemas}.", failed_schemas: failed_schemas)
84 defp message(%{reason: :min_length, length: length, name: name}) do
85 gettext("%{name} - String length is smaller than minLength: %{length}.",
91 defp message(%{reason: :max_length, length: length, name: name}) do
92 gettext("%{name} - String length is larger than maxLength: %{length}.",
98 defp message(%{reason: :unique_items, name: name}) do
99 gettext("%{name} - Array items must be unique.", name: name)
102 defp message(%{reason: :min_items, length: min, value: array, name: name}) do
103 gettext("%{name} - Array length %{length} is smaller than minItems: %{min}.",
105 length: length(array),
110 defp message(%{reason: :max_items, length: max, value: array, name: name}) do
111 gettext("%{name} - Array length %{length} is larger than maxItems: %{}.",
113 length: length(array),
118 defp message(%{reason: :multiple_of, length: multiple, value: count, name: name}) do
119 gettext("%{name} - %{count} is not a multiple of %{multiple}.",
126 defp message(%{reason: :exclusive_max, length: max, value: value, name: name})
128 gettext("%{name} - %{value} is larger than exclusive maximum %{max}.",
135 defp message(%{reason: :maximum, length: max, value: value, name: name})
137 gettext("%{name} - %{value} is larger than inclusive maximum %{max}.",
144 defp message(%{reason: :exclusive_multiple, length: min, value: value, name: name})
146 gettext("%{name} - %{value} is smaller than exclusive minimum %{min}.",
153 defp message(%{reason: :minimum, length: min, value: value, name: name})
155 gettext("%{name} - %{value} is smaller than inclusive minimum %{min}.",
162 defp message(%{reason: :invalid_type, type: type, value: value, name: name}) do
163 gettext("%{name} - Invalid %{type}. Got: %{value}.",
165 value: OpenApiSpex.TermType.type(value),
170 defp message(%{reason: :invalid_format, format: format, name: name}) do
171 gettext("%{name} - Invalid format. Expected %{format}.", name: name, format: inspect(format))
174 defp message(%{reason: :invalid_enum, name: name}) do
175 gettext("%{name} - Invalid value for enum.", name: name)
178 defp message(%{reason: :polymorphic_failed, type: polymorphic_type}) do
179 gettext("Failed to cast to any schema in %{polymorphic_type}",
180 polymorphic_type: polymorphic_type
184 defp message(%{reason: :unexpected_field, name: name}) do
185 gettext("Unexpected field: %{name}.", name: safe_string(name))
188 defp message(%{reason: :no_value_for_discriminator, name: field}) do
189 gettext("Value used as discriminator for `%{field}` matches no schemas.", name: field)
192 defp message(%{reason: :invalid_discriminator_value, name: field}) do
193 gettext("No value provided for required discriminator `%{field}`.", name: field)
196 defp message(%{reason: :unknown_schema, name: name}) do
197 gettext("Unknown schema: %{name}.", name: name)
200 defp message(%{reason: :missing_field, name: name}) do
201 gettext("Missing field: %{name}.", name: name)
204 defp message(%{reason: :missing_header, name: name}) do
205 gettext("Missing header: %{name}.", name: name)
208 defp message(%{reason: :invalid_header, name: name}) do
209 gettext("Invalid value for header: %{name}.", name: name)
212 defp message(%{reason: :max_properties, meta: meta}) do
214 "Object property count %{property_count} is greater than maxProperties: %{max_properties}.",
215 property_count: meta.property_count,
216 max_properties: meta.max_properties
220 defp message(%{reason: :min_properties, meta: meta}) do
222 "Object property count %{property_count} is less than minProperties: %{min_properties}",
223 property_count: meta.property_count,
224 min_properties: meta.min_properties
228 defp safe_string(string) do
229 to_string(string) |> String.slice(0..39)