Files
Brooke Kuhlmann 39414eb526 Refactored problem details within errors namespace
Cleans up the error objects so they are in the same namespace for quick lookup. This includes renaming `ProblemDetail` as `Problem` and using `petail` for `Petail` to clarify the differences.

Milestone: patch
2026-05-18 15:41:58 -06:00

63 lines
1.8 KiB
Ruby

# auto_register: false
# frozen_string_literal: true
require "functionable"
require "petail"
require "refinements/array"
require "refinements/hash"
module Terminus
module Aspects
module Errors
# Builds details for global problems.
module Problem
extend Functionable
using Refinements::Array
using Refinements::Hash
def duplicate message, instance
key, value = message.match(/Key \((?<key>[^)]+)\)=\((?<value>[^)]+)\)/m)
.named_captures
.values_at "key", "value"
Petail[
type: "/problem_details#duplicate_value",
status: :conflict,
detail: "#{key.capitalize} must be unique. " \
"Please use a value other than #{value.inspect}.",
instance:
]
end
def enum message, instance
key, value = message.match(/"(?<value>.+?)".+:(?<key>.+?)\s/)
.named_captures
.values_at "key", "value"
allowed = JSON(message[/\[".+?"\]/m]).to_usage :or
Petail[
type: "/problem_details#invalid_enum",
status: :unprocessable_content,
detail: "Invalid value for #{key}: #{value.inspect}. Use: #{allowed}.",
instance:
]
end
def foreign_key message, instance
key, value = message.match(/Key \((?<key>[^)]+)\)=\((?<value>[^)]+)\)/m)
.named_captures
.values_at "key", "value"
Petail[
type: "/problem_details#invalid_foreign_key",
status: :unprocessable_content,
detail: "Invalid `#{key}` value: #{value}. Does not exist.",
instance:
]
end
end
end
end
end