Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.05; 'subject:skip:s 10': 0.07; 'sys': 0.07; 'exception.': 0.09; 'exit': 0.09; 'exits': 0.09; 'http': 0.09; 'pep': 0.09; 'def': 0.12; 'assume': 0.14; "'200": 0.16; 'called,': 0.16; 'handling:': 0.16; 'subject:WSGI': 0.16; 'undesirable.': 0.16; 'url:peps': 0.16; 'wsgi': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'wed,': 0.18; 'feb': 0.22; 'import': 0.22; 'error': 0.23; 'case.': 0.24; 'gateway': 0.24; 'url:dev': 0.24; "i've": 0.25; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; 'catching': 0.31; 'exceptions': 0.31; 'url:python': 0.33; 'received:209.85': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'doing': 0.36; 'url:org': 0.36; 'so,': 0.37; 'received:209': 0.37; 'server': 0.38; 'handle': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'unable': 0.39; 'called': 0.40; 'such': 0.63; 'due': 0.66; 'here': 0.66; 'sample': 0.67; 'response.': 0.68; 'answer.': 0.68; '500': 0.70; '2015': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=P9idUwenUDbIJnQPliZKU+l1GXiXqYRVs4uwIfcaSwY=; b=Xyrs2r6SlOniSNyijm9MkfZ4c6nYzDUf5+4PlKldrE4bnus7VKKcUsFSb9BFcleLuz x0C+ZdUSb1i00HvhTwQpIy36rs0VV1lVKMUfahTClikQQ41MRAhW8ivNe3dspf9E4q0+ YDCKvg1COc4hLWs5LSUwi40Zb1T6sqGuTVl6ncXUrJi38+9c4YESEtiGB2drH1lkz3X3 z5PKzwsxGl/JbuvrChafPCSBe8ZSfFDyINSrLSD7J/uCzTKse1m5rudmwgH6V5h9MGs2 7m/3L2x2Jqu2TWCx+8jxqx4KajXT/U019In5KSmOTFG3RDWL9EnGPJBnTen/bAU9DKE/ uudw== X-Received: by 10.66.174.165 with SMTP id bt5mr112572pac.53.1424278315119; Wed, 18 Feb 2015 08:51:55 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <717f2dd0-7065-4b55-a536-9a40a6c8b291@googlegroups.com> References: <717f2dd0-7065-4b55-a536-9a40a6c8b291@googlegroups.com> From: Ian Kelly Date: Wed, 18 Feb 2015 09:51:14 -0700 Subject: Re: Assigning a function to sys.excepthook doesn't work in WSGI To: Python Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424278318 news.xs4all.nl 2894 [2001:888:2000:d::a6]:54132 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85799 On Wed, Feb 18, 2015 at 8:08 AM, wrote: > I want to generate an html page with http response status '200 OK' in cas= e of an uncaught exception in my wsgi application, instead of web server's = standard '500 Internal Server Error' response for such case. To do so, I've= made the following sample code: > > import sys > > def application(environ, start_response): > def exception(etype, evalue, trace): > start_response('200 OK', [('Content-Type', 'text/plain')]) > return ['Error'] > > sys.excepthook =3D exception > > 1/0 > > start_response('200 OK', [('Content-Type', 'text/plain')]) > return ['OK'] > > But the function 'exception' is never called, and a standard '500 Interna= l Server Error' response is still generated by server in case of an uncaugh= t exception. sys.excepthook is called just before the interpreter exits due to an exception. In a mod_wsgi environment, having the interpreter exit just because of an exception would be undesirable. I don't know exactly what it's doing under the hood, but I would assume that the exception never makes it to sys.excepthook because the gateway itself is catching the exception in order to generate the 500 response. > I looked through the documentation, but unable to find the answer. Are th= ere any ways to handle uncaught by try..except exceptions under mod_wsgi? Here is what PEP 3333 has to say about error handling: https://www.python.org/dev/peps/pep-3333/#error-handling