Skip to content

Save only error responses #846

@jdavid

Description

@jdavid

I wish to save only requests and responses with a 500 response status code.
Right now my solution is to use my own middleware and override _process_response:

    @transaction.atomic(using=router.db_for_write(models.SQLQuery))
    def _process_response(self, request, response):
        Logger.debug('Process response')
        with silk_meta_profiler():
            collector = DataCollector()
            collector.stop_python_profiler()
            silk_request = collector.request
            if silk_request:
                if response.status_code in settings.SILKY_INTERCEPT_STATUS_CODES:
                    ResponseModelFactory(response).construct_response_model()
                    silk_request.end_time = timezone.now()
                    collector.finalise()
                else:
                    silk_request.delete()
                    silk_request = None
            else:
                Logger.error(
                    'No request model was available when processing response. '
                    'Did something go wrong in process_request/process_view?'
                    '\n' + str(request) + '\n\n' + str(response)
                )
        # Need to save the data outside the silk_meta_profiler
        # Otherwise the  meta time collected in the context manager
        # is not taken in account
        if silk_request:
            silk_request.save()
        Logger.debug('Process response done.')

Then in my settings file:

SILKY_INTERCEPT_STATUS_CODES = {500}

It would be better if the request object was not created in the first place.
And if django-silk supported something like this out of the box.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions