<commit_before>package YourCompany::Plack::Error;

=encoding utf-8

=head1 NAME

YourCompany::Plack::Error

=head1 DESCRIPTION

L<Plack::Middleware::HTTPExceptions> friendly exception class.

Has code and messages, but returns errors instead of messages in JSON.

Additionally this class exports all L<HTTP::Status> constants.

Provides shortcuts like C<not_found> to L</throw> errors using error code.

=cut

use Mojo::Base -base;
use YourCompany::Perl::UTF8;

use HTTP::Status qw( HTTP_INTERNAL_SERVER_ERROR );
use Scalar::Util qw( blessed );
use Mojo::JSON qw( encode_json false );
use Exporter ();

use overload '""' => 'stringify';
use overload 'ne' => 'not_equal';
use overload 'eq' => 'equal';

has code  => HTTP_INTERNAL_SERVER_ERROR; # name to meet Plack::Middleware::HTTPExceptions

has messages => sub { return []; };

BEGIN {
    my $my_class = __PACKAGE__;

    no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
    for my $http_code ( grep { m/^HTTP_/ } @HTTP::Status::EXPORT_OK ) {
        my $sub_name = lc( $http_code );
        $sub_name =~ s/^http_//;
        *{"$my_class\::$sub_name"} = sub {
            my $class = shift;
            $class->throw( "HTTP::Status::$http_code"->(), @_);
        };
    }
    use strict 'refs';
}

sub message( $self, @values ) {
    if ( scalar @values ) {
        push @{ $self->messages }, @values;
        return $self;
    }
    return $self->stringify();
}

sub stringify {
    my $self = shift;
<commit_msg>Just use modern regexp replace returning.<commit_after>package YourCompany::Plack::Error;

=encoding utf-8

=head1 NAME

YourCompany::Plack::Error

=head1 DESCRIPTION

L<Plack::Middleware::HTTPExceptions> friendly exception class.

Has code and messages, but returns errors instead of messages in JSON.

Additionally this class exports all L<HTTP::Status> constants.

Provides shortcuts like C<not_found> to L</throw> errors using error code.

=cut

use Mojo::Base -base;
use YourCompany::Perl::UTF8;

use HTTP::Status qw( HTTP_INTERNAL_SERVER_ERROR );
use Scalar::Util qw( blessed );
use Mojo::JSON qw( encode_json false );
use Exporter ();

use overload '""' => 'stringify';
use overload 'ne' => 'not_equal';
use overload 'eq' => 'equal';

has code  => HTTP_INTERNAL_SERVER_ERROR; # name to meet Plack::Middleware::HTTPExceptions

has messages => sub { return []; };

BEGIN {
    my $my_class = __PACKAGE__;

    no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
    for my $http_code ( grep { m/^HTTP_/ } @HTTP::Status::EXPORT_OK ) {
        my $sub_name = ( $http_code =~ s/^HTTP_(.+)/\L$1\E/r );
        *{"$my_class\::$sub_name"} = sub {
            my $class = shift;
            $class->throw( "HTTP::Status::$http_code"->(), @_);
        };
    }
    use strict 'refs';
}

sub message( $self, @values ) {
    if ( scalar @values ) {
        push @{ $self->messages }, @values;
        return $self;
    }
    return $self->stringify();
}

sub stringify {
    my $self = shift;

{'fraction_non_alphanumeric': 0.14753004005340453, 'fraction_numerical': 0.0030040053404539386, 'mean_word_length': 3.6321483771251932, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2465030', 'n_tokens_mistral': 1010, 'n_tokens_neox': 985, 'n_words': 275}