Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»CodeSOD: An Exceptional Junior

    CodeSOD: An Exceptional Junior

    July 29, 2024

    When “dragoncoder047” was but a junior developer, without very much experience at all, they were tasked with building error handling in a Python Flask web application.

    Now, they were a junior, and tossed into the problem without much preparation, or much supervision, and just told to “make it work”. So they did. With this disaster:

    def _is_exception(obj):
    if type(obj) is TypeError: return True
    try:
    raise obj
    except BaseException as e:
    if type(e) is TypeError and str(e) == “exceptions must derive from BaseException”:
    return False
    else:
    return True
    raise Exception(“unreachable”)

    First, we compare type(obj) is TypeError. Is the type of obj TypeError? This is the wrong way to compare types, in general, as it doesn’t respect inheritance- isinstance(obj, TypeError) would be more appropriate, but then again, *this is an exception type. The try/except statement can filter by types for us, if we just throw the exception…

    Which is exactly what we do next. We raise obj inside of a try/except. Then we catch it, as a BaseException, the parent of all exceptions. Then, once again, we check to see if the exception is a TypeError and then also the message of the exception. Why?

    Because this is the exception raised when you throw something that isn’t actually an exception class. You’re not allowed to raise “an error occurred”, you must raise SomeExceptionTypeWithBaseExceptionAsAnAncestor(“an error occurred”).

    And that’s why they did the TypeError check above. If the incoming exception is already a TypeError, then we know it’s an exception. In that case, we return True. If it’s not a TypeError we throw it, knowing that throwing a non-exception object results in a TypeError with a specific message. So we can return False in that case. Otherwise, we can return True.

    Finally, we add one last exception throw to please the linter- despite the fact that we know that this will definitely return a value, the linter doesn’t, because this code is wrong on so many levels. So we ensure that it never reaches the end of the function without a return.

    All of this could be replaced with isinstance(obj, BaseException). But even if we did, we’re left with the question of: why do we need to do this at all? Using isinstance at all is a code smell in 90% of cases, and handling objects that are exception objects outside of some sort of try/catch block implies that we’ve gone way off somewhere.

    Well, our submitter explains why:

    The kicker is that the only place this function is used is in one single view function in a larger Flask web server. If the argument called “path” is actually an exception object, we render and return the error page template, else we do some other stuff with the actual path that was requested. And the reason that check is necessary is because I installed the same view function as both a request handler and an error handler — instead of just doing the obvious thing and just writing a dedicated error handler!

    It’s a case of baby’s first code-reuse creating bizarre code to try and glue unrelated functions into the same view. Sometimes the view shows actual data, sometimes it shows errors. So we have to do this test on the model to see if it’s actual data, or an error.

    As with many of our confessions, the developer’s heart was in the right place. They wanted to re-use code. They simply didn’t know how. And with no one giving them the support they needed, we got… this.

    [Advertisement]
    Continuously monitor your servers for configuration changes, and report when there’s configuration drift. Get started with Otter today!

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleExcessive Heat? Wearable Air Conditioners: The Future of Personalized Climate Control
    Next Article How to save an webpage as a xml file using Python and Selenium?

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 17, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-40906 – MongoDB BSON Serialization BSON::XS Multiple Vulnerabilities

    May 17, 2025
    Leave A Reply Cancel Reply

    Hostinger

    Continue Reading

    CVE-2025-4480 – Apache Code-Projects Simple College Management System Stack-Based Buffer Overflow Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Apache ActiveMQ Vulnerability Allows Remote Attackers to Execute Arbitrary Code

    Security

    Vietnamese Hacker Group Deploys New PXA Stealer Targeting Europe and Asia

    Development

    Improving health, one machine learning system at a time

    Artificial Intelligence

    Highlights

    Linux

    Rilasciata Netrunner 25 “Shockworm”: La Nuova Versione della Distribuzione GNU/Linux Basata su Debian

    February 11, 2025

    Dopo 2 anni di silenzio, la distribuzione GNU/Linux Netrunner, basata su Debian, ha finalmente rilasciato…

    AWS Inferentia and AWS Trainium deliver lowest cost to deploy Llama 3 models in Amazon SageMaker JumpStart

    May 2, 2024

    Getting started with cross-region inference in Amazon Bedrock

    August 29, 2024

    On-Chip Implementation of Backpropagation for Spiking Neural Networks on Neuromorphic Hardware

    November 26, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.