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

  • Home
  • SEARCH
  • 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 8120839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:16:58+00:00 2026-06-06T05:16:58+00:00

I wanted some system where I didn’t have to keep looking at the Exception

  • 0

I wanted some system where I didn’t have to keep looking at the Exception class and copying either status codes or text all at once. I also didn’t want to have to keep writing plain English error messages after catching exceptions. This seems to accomplish both, plus allows for easy IDE auto-completion!

Here’s some demo code:

<?php

class Thrive_URL_Exception extends Thrive_PrettyException
{
    const MISSING_CURL = "The cURL PHP extension must be enabled.";
    const MISSING_URL = "A URL has not been supplied or set.";
    const INVALID_URL = "The URL '%s' is not valid.";
    const BLANK_URL = "The URL '%s' contained no data. It is probably invalid.";
    const CONNECTION_TIMED_OUT = "The connection timed out.";
    const FILE_NOT_FOUND = "404: '%s' could not be found.";
    const NOT_ACCESSIBLE = "%d: '%s' is not currently accessible.";
    const PERMISSION_DENIED = "Permission denied.";
}

class Thrive_URL_Downloader
{
    public function fetch($url)
    {
        // Make sure the URL is valid.
        if (!self::isURLValid($url))
        {
            throw new Thrive_URL_Exception(Thrive_URL_Exception::INVALID_URL, array($url));
        }

        $ch = curl_init();
        curl_setopt_array($ch, array(CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_HEADERFUNCTION => array($this, 'captureHeader'),
                CURLOPT_TIMEOUT => 30,
            )
        );

        $data = curl_exec($ch);
        curl_close($ch);

        if ($data === false || is_null($data) || $data == '')
        {
            throw new Thrive_URL_Exception(Thrive_URL_Exception::BLANK_URL, array($url));
        }

        // TODO: Need to handle HTTP error messages, such as 404 and 502.
        $info = $this->getUrlInfo($ch);

        if ($info->httpCode == 401)
        {
            throw new Thrive_URL_Exception(Thrive_URL_Exception::PERMISSION_DENIED);
        }

        if ($info->httpCode == 404)
        {
            throw new Thrive_URL_Exception(Thrive_URL_Exception::FILE_NOT_FOUND, array($url));
        }

        if (in_array($info->httpCode, array(400, 401, 402, 403, 500, 501, 502, 503)))
        {
            throw new Thrive_URL_Exception(Thrive_URL_Exception::NOT_ACCESSIBLE, array($info->httpCode, $url));
        }

        $urlContent = new Thrive_Model_URLContent;
        $urlContent->url = $url;
        $urlContent->headers = $this->headers;
        $urlContent->info = $info;
        $urlContent->content = $data;

        return $urlContent;
    }
}

My question is whether there’s an obviously better way to do this sort of thing??

  • 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-06T05:17:00+00:00Added an answer on June 6, 2026 at 5:17 am

    IMHO it’s not best solution.

    Your exception class is breaking Single Responsibility Principle (SRP)­Wikipedia, because you are using same exception class for different kind of errors. What I’ll do is:

    Create single exception class for different kind of error:

     InvalidUrlException
     PermissionDeniedException
     FileNotFoundException (probalny this exception exists in core php)
    

    Then you can use excetion without passing any messages. The message is private part of class.

    For larger application this solution is better IMHO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LINQ generated class called Project. I wanted to add some attributes
hi I wanted to have some keys and values filtered out when compared with
I had a class with some common error handling code, and I wanted to
I have a problem with canvas i wanted to show some moving balls on
We are writing an inventory system and I have some questions about sqlalchemy (postgresql)
So I have some particular variables that mess up the content management system I'm
I am fixing some problems with a legacy system and have run into a
I just wanted some opinions from people that have run Selenium ( http://selenium.openqa.org )
I would like to learn ASP.NET and just wanted some input as to which
I know the topic I started is too subjective. But I just wanted some

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.