Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation check problem with custom exceptions #26

Open
char-pap opened this issue Dec 7, 2020 · 1 comment
Open

Validation check problem with custom exceptions #26

char-pap opened this issue Dec 7, 2020 · 1 comment

Comments

@char-pap
Copy link

char-pap commented Dec 7, 2020

There is a problem occuring with the validation checker. The current one:

def _check_validatation_data(self, data):
        detail_key = "detail"  
        if detail_key in data:  
            return False  
        return True

checks only for 'detail' inside the data and this creates a problem when a custom exception handling exists or even if some response actually has the key "detail" inside.

Proposal: Validate via the response status code. If it is in the range of HTTP 2xx it's True.

@Azmisov
Copy link

Azmisov commented Dec 18, 2024

I ran into this issue. My solution is to patch the renderer as:

class XLSXRendererPatch(XLSXRenderer):
    """ Modification on base XLSX renderer to prevent it from serializing exceptions as xlsx;
        see https://github.com/django-commons/drf-excel/issues/26
    """
    def render(self, data, accepted_media_type=None, renderer_context=None):
        if (
            (renderer_context and (ctx := renderer_context.get('response')) and ctx.status_code != 200) or
            (isinstance(data, dict) and ('error' in data or 'error_code' in data))
        ):
            self.media_type = 'application/json'
            return json.dumps(data)
        return super().render(data, accepted_media_type, renderer_context)

This checks HTTP 200 response, and also for non-presence of error/error_code. Additionally, I needed to change the media_type or it would send the default xlsx content type back for the JSONified error.

You may also need to get rid of XLSXFileMixin, or patch that as well, since it seems to try and fetch a filename for the content disposition header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants