Fix response code for nonexistent attachment
diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl
index 0c9d0ed..41bb741 100644
--- a/src/chttpd/src/chttpd_db.erl
+++ b/src/chttpd/src/chttpd_db.erl
@@ -1664,6 +1664,22 @@
end
end.
+get_attachment(Req, Db, DocId, FileName) ->
+ % Check if attachment exists
+ #doc_query_args{
+ rev=Rev,
+ options=Options
+ } = parse_doc_query(Req),
+ #doc{
+ atts=Atts
+ } = Doc = couch_doc_open(Db, DocId, Rev, Options),
+ case [A || A <- Atts, couch_att:fetch(name, A) == FileName] of
+ [] ->
+ {error, missing};
+ [Att] ->
+ {ok, Doc, Att}
+ end.
+
% Attachment request handlers
db_attachment_req(#httpd{method = 'GET', mochi_req = MochiReq} = Req, Db, DocId, FileNameParts) ->
@@ -1676,17 +1692,10 @@
"/"
)
),
- #doc_query_args{
- rev = Rev,
- options = Options
- } = parse_doc_query(Req),
- #doc{
- atts = Atts
- } = Doc = couch_doc_open(Db, DocId, Rev, Options),
- case [A || A <- Atts, couch_att:fetch(name, A) == FileName] of
- [] ->
+ case get_attachment(Req, Db, DocId, FileName) of
+ {error, missing} ->
throw({not_found, "Document is missing attachment"});
- [Att] ->
+ {ok, Doc, Att} ->
[Type, Enc, DiskLen, AttLen, Md5] = couch_att:fetch(
[type, encoding, disk_len, att_len, md5], Att
),
@@ -1822,7 +1831,12 @@
NewAtt =
case Method of
'DELETE' ->
- [];
+ case get_attachment(Req, Db, DocId, FileName) of
+ {error, missing} ->
+ throw({not_found, "Document is missing attachment"});
+ {ok, _, _} ->
+ []
+ end;
_ ->
MimeType =
case chttpd:header_value(Req, "Content-Type") of
diff --git a/test/elixir/test/attachments_test.exs b/test/elixir/test/attachments_test.exs
index 8e7f7d3..1f7c27a 100644
--- a/test/elixir/test/attachments_test.exs
+++ b/test/elixir/test/attachments_test.exs
@@ -109,6 +109,11 @@
resp = Couch.delete("/#{db_name}/bin_doc/foo.txt", query: %{w: 3})
assert resp.status_code == 409
+
+ resp = Couch.delete("/#{db_name}/bin_doc/notexisting.txt", query: %{w: 3, rev: rev})
+ assert resp.status_code == 404
+ assert resp.body["error"] == "not_found"
+ assert resp.body["reason"] == "Document is missing attachment"
resp = Couch.delete("/#{db_name}/bin_doc/foo.txt", query: %{w: 3, rev: rev})
assert resp.status_code == 200