Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8868665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:19:18+00:00 2026-06-14T17:19:18+00:00

I have an under statement that may generate and error (for instance, authentication error).

  • 0

I have an under statement that may generate and error (for instance, authentication error). I use content negotiation all over, and I’d like to return the error inside the under in a proper format. An example code:

under sub {
  my $self = shift;

  # Authenticated
  my $token = $self->param('token') || '';
  return 1 if $token eq '123456';

  # Not authenticated
  $self->respond_to(
    json => {
      json => { error => 'Invalid authentication token.' },
      status => 401
    },
    text => {
      text => 'Unauthorized.',
      status => 401
    }
  );
  return undef;
}

I can use render inside under, but respond_to won’t work. Probably under work for that. But in that case, what should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:19:19+00:00Added an answer on June 14, 2026 at 5:19 pm

    In Galileo I have an auth_fail helper which does something like this. While I work out an example, see that code (and the if_author and if_admin unders).

    Ok here is an example, the trick turned out to be (at least for this mechanism) before you can redirect to your failure handler, you need to flash the format, which make it available to the next handler.

    #!/usr/bin/env perl
    
    use Mojolicious::Lite;
    
    any '/fail' => sub {
      my $self = shift;
      $self->respond_to(
        json => {
          json => { error => 'Invalid authentication token.' },
          status => 401
        },
        text => {
          text => 'Unauthorized.',
          status => 401
        }
      );
    };
    
    under sub {
      my $self = shift;
    
      # Authenticated
      my $token = $self->param('token') || '';
      return 1 if $token eq '123456';
    
      # Not authenticated
      $self->flash( format => $self->param('format') );
      $self->redirect_to('fail');
      return undef;
    };
    
    any '/private' => sub {
      my $self = shift;
      $self->respond_to(
        json => {
          json => { launch_codes => '9999999' },
        },
        text => {
          text => 'Launch Code: 9999999',
        }
      ); 
    };
    
    app->start;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to make a gallery of all images i have under my domain
I may have a misunderstanding but I was under the impression that having a
I have many MySQL statements that are running under MySQL 4, but not MySQL
I have been under the assumption for a while that viewDidUnload is always called
I would like to have different applications under the same domain using Heroku. Because
I have some Oracle tables that represent a parent-child relationship. They look something like
I have a class that will normally run under Windows and uses the win32com
I have a switch statement in a crosstab query: Switch([Age]<20, Under 20, [Age]>=20 and
I have a read statement that expects a number, very simple example code: program
I have a simple if statement that doesn't seem to work. I'm out of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.