小编Ben*_*Ben的帖子

我是否可以使用HTTP状态代码的自定义原因来区分REST API的错误

我想区分不同类型的"未找到"错误.例如,给出以下请求:

GET /author/Adams/works/HHGTTG

作者可能"找不到"或作品可能"找不到",我想区分这两者.

状态:404 - 未找到作者
状态:404 - 找不到工作

根据规范,可以改变原因短语. http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html

6.1.1状态码和原因短语

......这里列出的原因只是建议 - 它们可以被当地的等价物替换而不影响协议......

对同一状态代码使用两个独特的短语也可以接受吗?

并且,这是一种合理的方法还是有更好的约定来表示更细粒度的错误?

最终,我希望有一个客户端库可以抛出AuthorNotFound或WorkNotFound异常,而不是通用的AuthorOrWorkNotFound异常.

rest http http-status-codes

16
推荐指数
2
解决办法
5162
查看次数

是否有一个内置函数可以一步对python列表进行排序和过滤?

给定一个带有数字名称的文件目录,我目前分两步对目录列表进行排序和过滤.

#files = os.listdir(path)
files = ["0", "1", "10", "5", "2", "11", "4", "15", "18", "14", "7", "8", "9"]

firstFile =  5
lastFile  = 15

#filter out any files that are not in the desired range
files = filter(lambda f: int(f) >= firstFile and int(f) < lastFile, files)

#sort the remaining files by timestamp
files.sort(lambda a,b: cmp(int(a), int(b)))
Run Code Online (Sandbox Code Playgroud)

是否有一个python函数结合了筛选和排序操作,所以列表只需要迭代一次?

python

8
推荐指数
1
解决办法
9231
查看次数

标签 统计

http ×1

http-status-codes ×1

python ×1

rest ×1