def _parse_error(hstore_str, pos):
    """format an unmarshalling error."""
    ctx = 20
    hslen = len(hstore_str)
    parsed_tail = hstore_str[max(pos - ctx - 1, 0):min(pos, hslen)]
    residual = hstore_str[min(pos, hslen):min(pos + ctx + 1, hslen)]
    if len(parsed_tail) > ctx:
        parsed_tail = '[...]' + parsed_tail[1:]
    if len(residual) > ctx:
        residual = residual[:-1] + '[...]'
    return 'After %r, could not parse residual at position %d: %r' % (
        parsed_tail, pos, residual)
