用于管理HTTP错误的模块

时间:2018-05-16 18:05:42

标签: perl http module

我正在编写Perl模块Result来管理HTTP错误。

我有点迷失了,无法生成管理这一切的功能。

Result.pm

package Result;

use strict;
use warnings;

use Data::Dumper;
use Log::Log4perl;

sub new
{
    my $class = shift;
    my %params = @_;

    my $self = {
        code      => $params{'code'},
        value     => $params{'value'}
    };

    bless $self, $class;
    return $self;
}

sub generateError
{
    my $self = shift;
    my %params = @_;

    if($self->{'code'} == 200)
    {
        return "Your request was well executed !";
    }
    elsif($self->{'code'} == 400)
    {
        return "There is an error in your request! Please verify it!";
    }
    elsif($self->{'code'} == 404)
    {
        return "We did not find for what you ask !"
    }
    elsif($self->{'code'} == 500)
    {
        return "Internal error of the server, please re-try later !";
    }
    elsif($self->{'code'} == 504)
    {
        return "Your request has set of time to be executed, please retry !";
    }
}

1;

App.pm

sub template
{
    my $self = shift;

    my $query   = $self->query;
    my $id = $query->param('id');

    my $session = $self->param('session');
    my $profile = $session->param('profile');

    my $Project = Project->newFromId($id);
    if(!$Project or $Project eq 'NOT_FOUND')
    {
        return $self->redirect('?rm=notfound');
    }

    if(!$profile->{'uid'} or $Project->{'userId'} != $profile->{'uid'})
    {
        return $self->redirect('?rm=notfound');
    }

    my $mailContent = from_json($Project->{'mail'});

    my $templateContent = formatTemplate($Project->{'template'});

    my $infos = [
        {
            'ID'        => $id,
            'TEMPLATE'  => $templateContent,
            'MAILSUBJECT'   => $mailContent->{'subject'},
            'MAILBODY'  => $mailContent->{'body'}
        }
    ];

    if($mailContent->{'type'} eq 'text')
    {
        $infos->[0]{'MAILTEXT'} = 1;
    }
    else
    {
        $infos->[0]{'MAILHTML'} = 1;
    }

    return $self->processtmpl('template.tmpl', $infos); 
}
1;

0 个答案:

没有答案