Code Monkey home page Code Monkey logo

Comments (12)

fcpauldiaz avatar fcpauldiaz commented on May 23, 2024

I can't get this to work. @kamilogorek What should be in the urls.py ?

from sentry-docs.

kamilogorek avatar kamilogorek commented on May 23, 2024

cc @mitsuhiko

from sentry-docs.

mitsuhiko avatar mitsuhiko commented on May 23, 2024

@fcpauldiaz i am not entirely sure what you are asking. Can you clarify what you mean with "can't get this to work"?

from sentry-docs.

fcpauldiaz avatar fcpauldiaz commented on May 23, 2024

@mitsuhiko The request.sentry object is still not available in my 500.html

This is my urls.py

from main import views as main_views
from django.conf.urls import  handler500 

handler500 = main_views.server_error

And tried this on the views.py

def server_error(request, template_name='500.html'):
    """500 error handler using RequestContext."""
    try:
        template = loader.get_template(template_name)
    except TemplateDoesNotExist:
        if template_name != '500.html':
            # Reraise if it's a missing custom template.
            raise
        return http.HttpResponseServerError('<h1>Server Error (500)</h1>', content_type='text/html')
    return http.HttpResponseServerError(template.render(None, request, status=500))

This is the templates/500.html

{% extends 'base.html' %}
{% block title %} Error {% endblock %}
{% block content %}
{% load raven %}
<div class="row container">
  <div class="col l10 offset-l3 s12 m8 offset-m2">
  <h3 class=lue-paypal-text center>You've encountered an error, oh noes!</h3>
  {{request.sentry}}
  {% if request.sentry.id %}
      <p>If you need assistance, you may reference this error as
      <strong>{{ request.sentry.id }}</strong>.</p>
  {% endif %}
  </div>
</div>
{% endblock %}

{% block js %}
<script>Raven.config('{% sentry_public_dsn %}').install()</script>

{% if request.sentry.id %}
  <script>
  Raven.showReportDialog({
    eventId: '{{ request.sentry.id }}',

    // use the public DSN (dont include your secret!)
    dsn: 'dns_here'
  });
  </script>
{% endif %}
{% endblock %}

from sentry-docs.

dcramer avatar dcramer commented on May 23, 2024

@fcpauldiaz im not sure if template.render ensures RequestContext is used, but its important that 'request' is made available to the template. If thats not happening this will never work. Additionally, if for some reason the 'Sentry' middleware isn't being installed, it also may not be available.

from sentry-docs.

fcpauldiaz avatar fcpauldiaz commented on May 23, 2024

This is my middleware config

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'raven.contrib.django.raven_compat.middleware.SentryResponseErrorIdMiddleware'
]

And the template processors

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'),],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'debug': DEBUG,
        },
    },
]

from sentry-docs.

dcramer avatar dcramer commented on May 23, 2024

@fcpauldiaz I dont know what Django does with your template.render call, so you need to verify that 'request' is actually being passed to the template. In older versions of Django that would not happen, and I dont know if the API has since changed (and dont have time to dig into their docs/implementation right now). That owuld be my best guess at whats wrong here.

from sentry-docs.

keimlink avatar keimlink commented on May 23, 2024

@dcramer That's exactly why started this issue: request is not passed to the template, so you need to use a custom server_error() view. My example works with Django 1.8 and Sentry in production.

from sentry-docs.

dcramer avatar dcramer commented on May 23, 2024

Yeah sorry was ignoring the originally issue. I'm not sure why we built the docs the way we did (oversight?), but there's a reasonable example in our generic docs:

https://docs.sentry.io/learn/user-feedback/

It's also important to note that Sentry should monkey-patch in a middleware which ensures some things happen. Without that none of this will work either. I can't imagine that isn't working so its likely just getting down to a fully functional custom_error function.

I'd also +1 shipping with this server_handler function, and possibly overwriting Django's default (though that might be a concern for future versions).

Alternatively, we can create a template tag so you dont need a custom function, and can simply {% load sentry %} {{ sentry.event_id }}

from sentry-docs.

keimlink avatar keimlink commented on May 23, 2024

Shipping Raven with a custom server_error() view sounds like an excellent idea. But why not leaving it up to the user if they want to use it by setting handler500 instead of monkey-patching Django? This would make it easier to handle changes to Django's server_error() view.

The setup instructions for Raven could explain that you either have to configure something like this in your urls.py or to use your own function.

from raven.views import server_error

handler500 = server_error

If Django's server_error() view is updated to pass the request to the template context only this part of the documentation has to be updated, but not the software itself.

Hope my explanation is clear enough to be understood…

from sentry-docs.

jaaved avatar jaaved commented on May 23, 2024

In Django 2.0.4 using this custom handler500 worked for me:

@requires_csrf_token
def sentry_server_error(request, template_name=ERROR_500_TEMPLATE_NAME):
    """
    500 error handler.

    Templates: :template:`500.html`
    Context: None
    """
    try:
        template = loader.get_template(template_name)
    except TemplateDoesNotExist:
        if template_name != ERROR_500_TEMPLATE_NAME:
            # Reraise if it's a missing custom template.
            raise
        return HttpResponseServerError('<h1>Server Error (500)</h1>', content_type='text/html')
    #return HttpResponseServerError(template.render(RequestContext(request)))
    return HttpResponseServerError(template.render(context=None, request=request))

from sentry-docs.

github-actions avatar github-actions commented on May 23, 2024

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Accepted, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

from sentry-docs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.