import contextlib

import flask


class FilterSchema:
    def __init__(self, fields: dict):
        self.fields = fields

    def parse(self):
        result = {}
        for name, field in self.fields.items():
            with contextlib.suppress(KeyError):
                result[name] = field.parse(name)
        return result


class FilterField:
    def __init__(self, *, field_name=None, parse_value=str):
        self.field_name_override = field_name
        self._parse_value = parse_value

    def parse(self, field_name):
        field_name = self.field_name_override or field_name
        value_string = flask.request.args['filter[{}]'.format(field_name)]
        return self.parse_value(value_string)

    def parse_value(self, value_string):
        return self._parse_value(value_string)


class ListFilterField(FilterField):
    def parse_value(self, value_string):
        return [self._parse_value(part) for part in value_string.split(',')]

import contextlib

import flask

from flask_jsonapi import exceptions


class FilterSchema:
    def __init__(self, fields: dict):
        self.fields = fields

    def parse(self):
        result = {}
        for name, field in self.fields.items():
            with contextlib.suppress(KeyError):
                result[name] = field.parse(name)
        return result


class FilterField:
    def __init__(self, *, field_name=None, parse_value=str):
        self.field_name_override = field_name
        self._parse_value = parse_value

    def parse(self, field_name):
        field_name = self.field_name_override or field_name
        value_string = flask.request.args['filter[{}]'.format(field_name)]
        try:
            return self.parse_value(value_string)
        except ValueError as e:
            raise exceptions.InvalidFilters('Error parsing {} filter: {}'.format(field_name, e))

    def parse_value(self, value_string):
        return self._parse_value(value_string)


class ListFilterField(FilterField):
    def parse_value(self, value_string):
        return [self._parse_value(part) for part in value_string.split(',')]

Return jsonapi error when parsing filters failed.
Return jsonapi error when parsing filters failed.

Change-Id: I4bd26823d9e29b31ab8fdc47b8ef2bb65071d27b
Reviewed-on: https://review.socialwifi.com/14069
Reviewed-by: Piotr Maliński <5f24252672783b9dd319151f284628d8f524ff27@socialwifi.com>
Tested-by: Jakub Skiepko <27b910087fee73ba587f81728e9a4e87eb24c8cc@socialwifi.com>

  import contextlib
  
  import flask
+ 
+ from flask_jsonapi import exceptions
  
  
  class FilterSchema:
      def __init__(self, fields: dict):
          self.fields = fields
  
      def parse(self):
          result = {}
          for name, field in self.fields.items():
              with contextlib.suppress(KeyError):
                  result[name] = field.parse(name)
          return result
  
  
  class FilterField:
      def __init__(self, *, field_name=None, parse_value=str):
          self.field_name_override = field_name
          self._parse_value = parse_value
  
      def parse(self, field_name):
          field_name = self.field_name_override or field_name
          value_string = flask.request.args['filter[{}]'.format(field_name)]
+         try:
-         return self.parse_value(value_string)
+             return self.parse_value(value_string)
+         except ValueError as e:
+             raise exceptions.InvalidFilters('Error parsing {} filter: {}'.format(field_name, e))
  
      def parse_value(self, value_string):
          return self._parse_value(value_string)
  
  
  class ListFilterField(FilterField):
      def parse_value(self, value_string):
          return [self._parse_value(part) for part in value_string.split(',')]
  
Return jsonapi error when parsing filters failed.
## Code Before:
import contextlib

import flask


class FilterSchema:
    def __init__(self, fields: dict):
        self.fields = fields

    def parse(self):
        result = {}
        for name, field in self.fields.items():
            with contextlib.suppress(KeyError):
                result[name] = field.parse(name)
        return result


class FilterField:
    def __init__(self, *, field_name=None, parse_value=str):
        self.field_name_override = field_name
        self._parse_value = parse_value

    def parse(self, field_name):
        field_name = self.field_name_override or field_name
        value_string = flask.request.args['filter[{}]'.format(field_name)]
        return self.parse_value(value_string)

    def parse_value(self, value_string):
        return self._parse_value(value_string)


class ListFilterField(FilterField):
    def parse_value(self, value_string):
        return [self._parse_value(part) for part in value_string.split(',')]

## Instruction:
Return jsonapi error when parsing filters failed.
## Code After:
import contextlib

import flask

from flask_jsonapi import exceptions


class FilterSchema:
    def __init__(self, fields: dict):
        self.fields = fields

    def parse(self):
        result = {}
        for name, field in self.fields.items():
            with contextlib.suppress(KeyError):
                result[name] = field.parse(name)
        return result


class FilterField:
    def __init__(self, *, field_name=None, parse_value=str):
        self.field_name_override = field_name
        self._parse_value = parse_value

    def parse(self, field_name):
        field_name = self.field_name_override or field_name
        value_string = flask.request.args['filter[{}]'.format(field_name)]
        try:
            return self.parse_value(value_string)
        except ValueError as e:
            raise exceptions.InvalidFilters('Error parsing {} filter: {}'.format(field_name, e))

    def parse_value(self, value_string):
        return self._parse_value(value_string)


class ListFilterField(FilterField):
    def parse_value(self, value_string):
        return [self._parse_value(part) for part in value_string.split(',')]
