Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CouchDB error on specific document: "function_clause error in HTTP request"

I have a CouchDB database in which I store documents with PDF and PNG attachments. After saving an attachment on a particular document, the show function fails when trying to view that document in the browser. The message in the browser is:

{"error":"unknown_error","reason":"function_clause"}

The error in couch.log is

[Wed, 08 Jun 2011 21:29:58 GMT] [error] [<0.106.0>] function_clause error in HTTP request

[Wed, 08 Jun 2011 21:29:58 GMT] [info] [<0.106.0>] Stacktrace: [{couch_httpd,doc_etag,
             [{'EXIT',
                  {{case_clause,
                       {[{<<"author">>,
                          [<<"Brian St. Claire-King">>,
                           <<"Eloy LaSanta">>]},
                         {<<"book_type">>,[<<"Roleplaying Game">>]},
                         {<<"date_added">>,[2011,6,7]},
                         {<<"date_published">>,2008},
                         {<<"genre">>,
                          [<<"Futuristic">>,<<"Post-Apocalyptic">>]},
                         {<<"language">>,<<"English">>},
                         {<<"license">>,
                          [{[{<<"url">>,<<"http://www.lulu.com">>},
                             {<<"id">>,<<"4880508">>}]}]},
                         {<<"page_count">>,276},
                         {<<"publisher">>,<<"Vajra Enterprises">>},
                         {<<"subtitle">>,<<"The Role Playing Game">>},
                         {<<"system">>,[<<"Organic Rule Components">>]},
                         {<<"title">>,<<"KidWorld">>},
                         {<<"last_modified">>,
                          <<"2011-06-08T01:58:44.697Z">>},
                         {<<"publisher_url">>,
                          <<"http://www.vajraenterprises.com/">>}]}},
                   [{couch_btree,lookup,3},
                    {couch_btree,lookup_kpnode,5},
                    {couch_btree,lookup,2},
                    {couch_db,get_full_doc_info,2},
                    {couch_db,open_doc_int,3},
                    {couch_db,open_doc,3},
                    {couch_httpd_db,couch_doc_open,4},
                    {couch_httpd_show,maybe_open_doc,2}]}}]},
         {couch_httpd_show,show_etag,4},
         {couch_httpd_show,handle_doc_show,6},
         {couch_httpd_db,do_db_req,2},
         {couch_httpd,handle_request_int,5},
         {mochiweb_http,headers,5},
         {proc_lib,init_p_do_apply,3}]

[Wed, 08 Jun 2011 21:29:58 GMT] [info] [<0.106.0>] 127.0.0.1 - - 'GET' /bookshelf/_design/bookshelf_loader/_show/book/4ddbbd8f3fd780c65e03c0a5e901fd57 500

I can only guess that one of the attachments is corrupt. I have tried running the tests in the Futon Test Suite that are applicable to attachments; the "attachments" test ran for over 12 hours without completing.

I would just delete the document but I don't have a revision number and any attempt to access that document fails. I tried querying the _all_docs view with cURL, but it was interrupted half-way with the error:

{"id":"40af854814db1b4890c37c669a11de28","key":"40af854814db1b4890c37c669a11de28","value":{"rev":"4-ff85b5dbb2e4800ab4bfee81f32f0866"}}
curl: (56) Received problem 2 in the chunky parser

Is there any way that I can delete the offending document (or its attachments) knowing only the _id and not the _rev?

Edit: I paged back through the log and found the initial crash, as below:

[Wed, 08 Jun 2011 02:00:05 GMT] [error] [<0.679.0>] ** Generic server <0.679.0> terminating 
** Last message in was {update_docs,<0.4902.0>,
                       [[{doc,<<"4ddbbd8f3fd780c65e03c0a5e901fd57">>,
                             {4,
                              [<<81,240,217,60,28,224,204,196,19,243,27,
                                 111,230,88,90,205>>,
                               <<171,3,185,48,48,23,182,2,95,122,48,132,
                                 184,105,254,242>>,
                               <<69,27,210,166,21,121,125,192,47,237,187,
                                 37,74,195,109,22>>,
                               <<145,31,9,71,4,24,15,90,97,154,177,102,
                                 151,156,111,34>>]},
                             {[{<<"author">>,
                                [<<"Brian St. Claire-King">>,
                                 <<"Eloy LaSanta">>]},
                               {<<"book_type">>,[<<"Roleplaying Game">>]},
                               {<<"date_added">>,[2011,6,7]},
                               {<<"date_published">>,2008},
                               {<<"genre">>,
                                [<<"Futuristic">>,<<"Post-Apocalyptic">>]},
                               {<<"language">>,<<"English">>},
                               {<<"license">>,
                                [{[{<<"url">>,<<"http://www.lulu.com">>},
                                   {<<"id">>,<<"4880508">>}]}]},
                               {<<"page_count">>,276},
                               {<<"publisher">>,<<"Vajra Enterprises">>},
                               {<<"subtitle">>,
                                <<"The Role Playing Game">>},
                               {<<"system">>,
                                [<<"Organic Rule Components">>]},
                               {<<"title">>,<<"KidWorld">>},
                               {<<"last_modified">>,
                                <<"2011-06-08T01:58:44.697Z">>},
                               {<<"publisher_url">>,
                                <<"http://www.vajraenterprises.com/">>}]},
                             [{att,<<"KidWorld_2010_for_pdf.pdf">>,
                                  <<"application/pdf">>,76729048,76729048,
                                  <<143,119,231,165,202,3,166,134,23,195,
                                    94,84,227,25,87,193>>,
                                  4,
                                  {<0.676.0>,[4280434790]},
                                  identity},
                              {att,<<"kidworld copy.png">>,
                                  <<"image/png">>,21674,21674,
                                  <<237,107,129,30,82,36,136,61,29,17,202,
                                    217,209,88,187,125>>,
                                  3,
                                  {<0.676.0>,[4280406118]},
                                  identity}],
                             false,[]}]],
                       [],false,false}
** When Server state == {db,<0.678.0>,<0.679.0>,nil,<<"1307486659185780">>,
                        <0.676.0>,<0.680.0>,
                        {db_header,5,2454,0,
                            {4280430351,{508,9}},
                            {4280431672,517},
                            nil,0,nil,nil,1000},
                        2454,
                        {btree,<0.676.0>,
                            {4280430351,{508,9}},
                            #Fun<couch_db_updater.7.69395062>,
                            #Fun<couch_db_updater.8.86519079>,
                            #Fun<couch_btree.5.124754102>,
                            #Fun<couch_db_updater.9.24674233>},
                        {btree,<0.676.0>,
                            {4280431672,517},
                            #Fun<couch_db_updater.10.90337910>,
                            #Fun<couch_db_updater.11.13595824>,
                            #Fun<couch_btree.5.124754102>,
                            #Fun<couch_db_updater.12.34906778>},
                        {btree,<0.676.0>,nil,
                            #Fun<couch_btree.0.83553141>,
                            #Fun<couch_btree.1.30790806>,
                            #Fun<couch_btree.2.124754102>,nil},
                        2454,<<"bookshelf">>,
                        "../var/lib/couchdb/bookshelf.couch",[],[],nil,
                        {user_ctx,null,[],undefined},
                        nil,1000,
                        [before_header,after_header,on_file_open],
                        false}
** Reason for termination == 
** {{case_clause,{[{<<"author">>,
                [<<"Brian St. Claire-King">>,<<"Eloy LaSanta">>]},
               {<<"book_type">>,[<<"Roleplaying Game">>]},
               {<<"date_added">>,[2011,6,7]},
               {<<"date_published">>,2008},
               {<<"genre">>,[<<"Futuristic">>,<<"Post-Apocalyptic">>]},
               {<<"language">>,<<"English">>},
               {<<"license">>,
                [{[{<<"url">>,<<"http://www.lulu.com">>},
                   {<<"id">>,<<"4880508">>}]}]},
               {<<"page_count">>,276},
               {<<"publisher">>,<<"Vajra Enterprises">>},
               {<<"subtitle">>,<<"The Role Playing Game">>},
               {<<"system">>,[<<"Organic Rule Components">>]},
               {<<"title">>,<<"KidWorld">>},
               {<<"last_modified">>,<<"2011-06-08T01:58:44.697Z">>},
               {<<"publisher_url">>,
                <<"http://www.vajraenterprises.com/">>}]}},
[{couch_btree,modify_node,4},
 {couch_btree,modify_kpnode,6},
 {couch_btree,modify_node,4},
 {couch_btree,query_modify,4},
 {couch_btree,add_remove,3},
 {couch_db_updater,update_docs_int,5},
 {couch_db_updater,handle_info,2},
 {gen_server,handle_msg,5}]}


[Wed, 08 Jun 2011 02:00:06 GMT] [error] [<0.679.0>] {error_report,<0.33.0>,
{<0.679.0>,crash_report,
 [[{initial_call,{couch_db_updater,init,['Argument__1']}},
   {pid,<0.679.0>},
   {registered_name,[]},
   {error_info,
       {exit,
           {{case_clause,
                {[{<<"author">>,
                   [<<"Brian St. Claire-King">>,<<"Eloy LaSanta">>]},
                  {<<"book_type">>,[<<"Roleplaying Game">>]},
                  {<<"date_added">>,[2011,6,7]},
                  {<<"date_published">>,2008},
                  {<<"genre">>,[<<"Futuristic">>,<<"Post-Apocalyptic">>]},
                  {<<"language">>,<<"English">>},
                  {<<"license">>,
                   [{[{<<"url">>,<<"http://www.lulu.com">>},
                      {<<"id">>,<<"4880508">>}]}]},
                  {<<"page_count">>,276},
                  {<<"publisher">>,<<"Vajra Enterprises">>},
                  {<<"subtitle">>,<<"The Role Playing Game">>},
                  {<<"system">>,[<<"Organic Rule Components">>]},
                  {<<"title">>,<<"KidWorld">>},
                  {<<"last_modified">>,<<"2011-06-08T01:58:44.697Z">>},
                  {<<"publisher_url">>,
                   <<"http://www.vajraenterprises.com/">>}]}},
            [{couch_btree,modify_node,4},
             {couch_btree,modify_kpnode,6},
             {couch_btree,modify_node,4},
             {couch_btree,query_modify,4},
             {couch_btree,add_remove,3},
             {couch_db_updater,update_docs_int,5},
             {couch_db_updater,handle_info,2},
             {gen_server,handle_msg,5}]},
           [{gen_server,terminate,6},{proc_lib,init_p_do_apply,3}]}},
   {ancestors,[<0.678.0>,<0.675.0>]},
   {messages,[]},
   {links,[<0.678.0>]},
   {dictionary,[]},
   {trap_exit,true},
   {status,running},
   {heap_size,6765},
   {stack_size,24},
   {reductions,65012}],
  []]}}

This is my _show function. I don't have any _update functions.

function(doc, req) {  
  var ddoc = this;
  var Mustache = require("lib/mustache");
  var path = require("vendor/couchapp/lib/path").init(req);
  var indexPath = path.list('index','recent-books',{descending:true, limit:25});

  var data = {
    header : {
      index : indexPath,
      headerTitle : "Bookshelf"
    },
    scripts : {
        assets : path.asset()
    },
index : indexPath,
    pageTitle : doc ? "Edit: " + doc.title : "Create a new book",
    assets : path.asset(),
db : req.path[0]
  };

  if (doc) {
    data.doc = JSON.stringify(doc);
    data.author = doc.author;
    data.book_types = getCheckboxValues(bookTypeArray, doc.book_type);
    data.book_type_other = getOthers(bookTypeArray, doc.book_type);
    data.cover_image = getAttachmentName(doc._attachments, "image/png");
    data.date_added = getDateString(doc.date_added);        
    data.date_published = doc.date_published;
    data.edition = doc.edition;
    data.issue_number = doc.issue_number;
    data.file_name = getAttachmentName(doc._attachments, "application/pdf");
    data.genres = getCheckboxValues(genreArray, doc.genre);
    data.genre_other = getOthers(genreArray, doc.genre);
    data._id = doc._id;
    data.languages = getDropdownValues(languageArray, doc.language);
    data.licenses = getLicenses(doc.license);
    data.page_count = doc.page_count;
    data.publishers = getDropdownValues(publisherArray, doc.publisher);
    data.publisher_other = getOther(publisherArray, doc.publisher);
    data.publisher_url = doc.publisher_url;
    data._rev = doc._rev;
    data.subtitle = doc.subtitle;
    data.systems = getCheckboxValues(systemArray, doc.system);
    data.system_other = getOthers(systemArray, doc.system);
    data.tags = doc.tags;
    data.title = doc.title;
    data.version = doc.version;
  } else {
    data.doc = JSON.stringify({});
    data.date_added = getDateString();
    data.book_types = getCheckboxValues(bookTypeArray);
    data.languages = getDropdownValues(languageArray, "English");
    data.licenses = "http://www.drivethrurpg.com,";
    data.publishers = getDropdownValues(publisherArray);
    data.genres = getCheckboxValues(genreArray);
    data.systems = getCheckboxValues(systemArray);
  }
  return Mustache.to_html(ddoc.templates.edit, data, ddoc.templates.partials);
}

I didn't paste all of the relatively simple helper functions (e.g. getCheckboxValues) that translate between what is stored in Couch and what goes on the screen, but I can if you think they'd be salutary.

I was finally able to access an older view and get the _rev of the document. Issuing the DELETE command with cURL results in the same "case_clause" error as before, however. Any attempt to PUT or POST a new document into the database will hang indefinitely and the database becomes inaccessible from that point on. (Fortunately I have a backup copy, which still has the one corrupt file but is at least accessible.)

like image 942
oroborous Avatar asked Jun 08 '11 22:06

oroborous


1 Answers

As @JasonSmith mentioned in the comments above, this can sometimes be caused by a bug in your Javascript. I stumbled upon this same error message by accidentally setting a View's name property to a function directly rather than to an object containing its map and optional reduce functions. AKA

Bad:

"views": {
    "test_view": "function (doc) { ... }"
 }

Good:

"views": {
   "test_view": {
       "map": "function (doc) { ... }"
   }
}
like image 137
Dom Vinyard Avatar answered Sep 23 '22 13:09

Dom Vinyard