Reject design docs with compilation errors If we know the language for a given design doc we'll attempt to compile the map/reduce functions and reject updates that introduce code that doesn't compile. We don't yet do the other types of functions because those errors are more user visible.
Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/dc5a6de3 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/dc5a6de3 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/dc5a6de3 Branch: refs/heads/import Commit: dc5a6de321210bc7a3387ad71a2ab2ce9f6abf46 Parents: 7b433b8 Author: Paul J. Davis <[email protected]> Authored: Sun Mar 10 16:03:00 2013 -0500 Committer: Paul J. Davis <[email protected]> Committed: Fri Jan 17 16:44:31 2014 -0800 ---------------------------------------------------------------------- src/couch_db.erl | 15 +++++++++++++-- src/couch_query_servers.erl | 12 ++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/dc5a6de3/src/couch_db.erl ---------------------------------------------------------------------- diff --git a/src/couch_db.erl b/src/couch_db.erl index 56cb0d2..7734c7c 100644 --- a/src/couch_db.erl +++ b/src/couch_db.erl @@ -502,8 +502,11 @@ group_alike_docs([{Doc,Ref}|Rest], [Bucket|RestBuckets]) -> group_alike_docs(Rest, [[{Doc,Ref}]|[Bucket|RestBuckets]]) end. -validate_doc_update(#db{}=Db, #doc{id= <<"_design/",_/binary>>}, _GetDiskDocFun) -> - catch check_is_admin(Db); +validate_doc_update(#db{}=Db, #doc{id= <<"_design/",_/binary>>}=Doc, _GetDiskDocFun) -> + case catch check_is_admin(Db) of + ok -> validate_ddoc(Db#db.name, Doc); + Error -> Error + end; validate_doc_update(#db{validate_doc_funs = undefined} = Db, Doc, Fun) -> ValidationFuns = load_validation_funs(Db), validate_doc_update(Db#db{validate_doc_funs=ValidationFuns}, Doc, Fun); @@ -519,6 +522,14 @@ validate_doc_update(Db, Doc, GetDiskDocFun) -> validate_doc_update_int(Db, Doc, GetDiskDocFun) end. +validate_ddoc(DbName, DDoc) -> + try + couch_index_server:validate(DbName, couch_doc:with_ejson_body(DDoc)) + catch + throw:Error -> + Error + end. + validate_doc_update_int(Db, Doc, GetDiskDocFun) -> DiskDoc = GetDiskDocFun(), JsonCtx = couch_util:json_user_ctx(Db), http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/dc5a6de3/src/couch_query_servers.erl ---------------------------------------------------------------------- diff --git a/src/couch_query_servers.erl b/src/couch_query_servers.erl index d919a85..8e4130e 100644 --- a/src/couch_query_servers.erl +++ b/src/couch_query_servers.erl @@ -12,6 +12,7 @@ -module(couch_query_servers). +-export([try_compile/4]). -export([start_doc_map/3, map_docs/2, map_docs_raw/2, stop_doc_map/1, raw_to_ejson/1]). -export([reduce/3, rereduce/3,validate_doc_update/5]). -export([filter_docs/5]). @@ -65,6 +66,17 @@ 46,65,9,27,108,27,101,27,110,27,103,27,116,27,104,56,9,34,59,84,0,78,84,1,102, 65,9,27,125,56,9,84,1,206,0,0,0,0,0,0,0>>}). + +try_compile(Proc, FunctionType, FunctionName, FunctionSource) -> + try + proc_prompt(Proc, [<<"add_fun">>, FunctionSource]), + ok + catch {compilation_error, E} -> + Fmt = "Compilation of the ~s function in the '~s' view failed: ~s", + Msg = io_lib:format(Fmt, [FunctionType, FunctionName, E]), + throw({compilation_error, Msg}) + end. + start_doc_map(Lang, Functions, Lib) -> Proc = get_os_process(Lang), case Lib of
