# Buggy code
def _raise_412_if_modified(self, record=None):
    """Raise 412 if current timestamp is superior to the one
    specified in headers.

    :raises:
        :exc:`~pyramid:pyramid.httpexceptions.HTTPPreconditionFailed`
    """
    if_match = self.request.headers.get("If-Match")
    if_none_match = self.request.headers.get("If-None-Match")

    if not if_match and not if_none_match:
        return

    if_match = decode_header(if_match) if if_match else None

    if record and if_none_match and decode_header(if_none_match) == "*":
        if record.get(self.model.deleted_field, False):
            # Tombstones should not prevent creation.
            return
        modified_since = -1  # Always raise.
    elif if_match:
        try:
            if not (if_match[0] == if_match[-1] == '"'):
                raise ValueError()
            modified_since = int(if_match[1:-1])
        except (IndexError, ValueError):
            message = (
                "Invalid value for If-Match. The value should "
                "be integer between double quotes."
            )
            error_details = {"location": "header", "description": message}
            raise_invalid(self.request, **error_details)
    else:
        # In case _raise_304_if_not_modified() did not raise.
        return

    if record:
        current_timestamp = record[self.model.modified_field]
    else:
        current_timestamp = self.model.timestamp()

    if current_timestamp > modified_since:
        error_msg = "Resource was modified meanwhile"
        details = {"existing": record} if record else {}
        response = http_error(
            HTTPPreconditionFailed(),
            errno=ERRORS.MODIFIED_MEANWHILE,
            message=error_msg,
            details=details,
        )
        self._add_timestamp_header(response, timestamp=current_timestamp)
        raise response


# Traceback (UnicodeDecodeError)
Traceback (most recent call last):
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/tweens.py\", line 22, in excview_tween
response = handler(request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid_tm/__init__.py\", line 119, in tm_tween
reraise(*exc_info)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid_tm/__init__.py\", line 98, in tm_tween
response = handler(request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/router.py\", line 158, in handle_request
view_name
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/view.py\", line 547, in _call_view
response = view_callable(context, request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/config/views.py\", line 182, in __call__
return view(context, request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/viewderivers.py\", line 393, in attr_view
return view(context, request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/viewderivers.py\", line 371, in predicate_wrapper
return view(context, request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/viewderivers.py\", line 302, in _secured_view
return view(context, request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/viewderivers.py\", line 442, in rendered_view
result = view(context, request)
File \"/data/kinto-dist/lib/python2.7/site-packages/pyramid/viewderivers.py\", line 147, in _requestonly_view
response = view(request)
File \"/data/kinto-dist/lib/python2.7/site-packages/cornice/service.py\", line 571, in wrapper
response = view_()
File \"/data/kinto-dist/lib/python2.7/site-packages/kinto/views/records.py\", line 78, in collection_get
result = super(Record, self).collection_get()
File \"/data/kinto-dist/lib/python2.7/site-packages/kinto/core/resource/__init__.py\", line 249, in collection_get
self._raise_304_if_not_modified()
File \"/data/kinto-dist/lib/python2.7/site-packages/kinto/core/resource/__init__.py\", line 744, in _raise_304_if_not_modified
if_none_match = decode_header(if_none_match)
File \"/data/kinto-dist/lib/python2.7/site-packages/kinto/core/utils.py\", line 394, in decode_header
value = value.decode(encoding)
File \"/data/kinto-dist/lib64/python2.7/encodings/utf_8.py\", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: \'utf8\' codec can\'t decode byte 0xea in position 15: invalid continuation

# Fixed code
def _raise_412_if_modified(self, record=None):
    """Raise 412 if current timestamp is superior to the one
    specified in headers.

    :raises:
        :exc:`~pyramid:pyramid.httpexceptions.HTTPPreconditionFailed`
    """
    if_match = self.request.headers.get("If-Match")
    if_none_match = self.request.headers.get("If-None-Match")

    if not if_match and not if_none_match:
        return

    error_details = {
        "location": "header",
        "description": (
            "Invalid value for If-Match. The value should "
            "be integer between double quotes."
        ),
    }

    try:
        if_match = decode_header(if_match) if if_match else None
        if_none_match = decode_header(if_none_match) if if_none_match else None
    except UnicodeDecodeError:
        raise_invalid(self.request, **error_details)

    if record and if_none_match == "*":
        if record.get(self.model.deleted_field, False):
            # Tombstones should not prevent creation.
            return
        modified_since = -1  # Always raise.
    elif if_match:
        try:
            if not (if_match[0] == if_match[-1] == '"'):
                raise ValueError()
            modified_since = int(if_match[1:-1])
        except (IndexError, ValueError):
            raise_invalid(self.request, **error_details)
    else:
        # In case _raise_304_if_not_modified() did not raise.
        return

    if record:
        current_timestamp = record[self.model.modified_field]
    else:
        current_timestamp = self.model.timestamp()

    if current_timestamp > modified_since:
        error_msg = "Resource was modified meanwhile"
        details = {"existing": record} if record else {}
        response = http_error(
            HTTPPreconditionFailed(),
            errno=ERRORS.MODIFIED_MEANWHILE,
            message=error_msg,
            details=details,
        )
        self._add_timestamp_header(response, timestamp=current_timestamp)
        raise response