sul-dlss-deprecated / triannon

Rails engine for working with storage of OpenAnnotations stored in Fedora4
Other
13 stars 1 forks source link

POST generates internal server error 500 #139

Closed dazza-codes closed 9 years ago

dazza-codes commented 9 years ago

Ran triannon using rake triannon:server_jetty and the console trace contains:

Started POST "/annotations/" for 127.0.0.1 at 2015-03-17 17:32:28 -0700
Processing by Triannon::AnnotationsController#create as XML
  Parameters: {"@context"=>{"oa"=>"http://www.w3.org/ns/oa#", "cnt"=>"http://www.w3.org/2011/content#", "dc"=>"http://purl.org/dc/elements/1.1/", "dcterms"=>"http://purl.org/dc/terms/", "dctypes"=>"http://purl.org/dc/dcmitype/", "foaf"=>"http://xmlns.com/foaf/0.1/", "rdf"=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdfs"=>"http://www.w3.org/2000/01/rdf-schema#", "skos"=>"http://www.w3.org/2004/02/skos/core#", "owl"=>"http://www.w3.org/2002/07/owl#", "prov"=>"http://www.w3.org/ns/prov#", "trig"=>"http://www.w3.org/2004/03/trix/rdfg-1/", "xsd"=>"http://www.w3.org/2001/XMLSchema#", "hasBody"=>{"@type"=>"@id", "@id"=>"oa:hasBody"}, "hasTarget"=>{"@type"=>"@id", "@id"=>"oa:hasTarget"}, "hasSource"=>{"@type"=>"@id", "@id"=>"oa:hasSource"}, "hasSelector"=>{"@type"=>"@id", "@id"=>"oa:hasSelector"}, "hasState"=>{"@type"=>"@id", "@id"=>"oa:hasState"}, "hasScope"=>{"@type"=>"@id", "@id"=>"oa:hasScope"}, "annotatedBy"=>{"@type"=>"@id", "@id"=>"oa:annotatedBy"}, "serializedBy"=>{"@type"=>"@id", "@id"=>"oa:serializedBy"}, "motivatedBy"=>{"@type"=>"@id", "@id"=>"oa:motivatedBy"}, "equivalentTo"=>{"@type"=>"@id", "@id"=>"oa:equivalentTo"}, "styledBy"=>{"@type"=>"@id", "@id"=>"oa:styledBy"}, "cachedSource"=>{"@type"=>"@id", "@id"=>"oa:cachedSource"}, "conformsTo"=>{"@type"=>"@id", "@id"=>"dcterms:conformsTo"}, "default"=>{"@type"=>"@id", "@id"=>"oa:default"}, "item"=>{"@type"=>"@id", "@id"=>"oa:item"}, "first"=>{"@type"=>"@id", "@id"=>"rdf:first"}, "rest"=>{"@type"=>"@id", "@id"=>"rdf:rest", "@container"=>"@list"}, "annotatedAt"=>{"@type"=>"xsd:dateTimeStamp", "@id"=>"oa:annotatedAt"}, "end"=>{"@type"=>"xsd:nonNegativeInteger", "@id"=>"oa:end"}, "exact"=>"oa:exact", "prefix"=>"oa:prefix", "serializedAt"=>{"@type"=>"xsd:dateTimeStamp", "@id"=>"oa:serializedAt"}, "start"=>{"@type"=>"xsd:nonNegativeInteger", "@id"=>"oa:start"}, "styleClass"=>"oa:styleClass", "suffix"=>"oa:suffix", "when"=>{"@type"=>"xsd:dateTimeStamp", "@id"=>"oa:when"}, "chars"=>"cnt:chars", "bytes"=>"cnt:bytes", "format"=>"dc:format", "language"=>"dc:language", "value"=>"rdf:value", "label"=>"rdfs:label", "name"=>"foaf:name", "mbox"=>"foaf:mbox"}, "@graph"=>[{"@id"=>"_:b92", "@type"=>"oa:Annotation", "hasBody"=>"_:b93", "hasTarget"=>"http://dms-data.stanford.edu/data/manifests/BnF/jr903ng8662/canvas/canvas-60#xywh=441,3599,1363,58", "motivatedBy"=>"sc:painting", "oa:serializedAt"=>{"@value"=>"2015-03-18T00:32:28Z", "@type"=>"xsd:dateTime"}, "serializedBy"=>"https://github.com/sul-dlss/annotations2triannon"}, {"@id"=>"_:b93", "@type"=>"cnt:ContentAsText", "chars"=>"Ne deffaites ou desdittes", "format"=>"text/plain", "language"=>"frm"}], "annotation"=>{"@context"=>{"oa"=>"http://www.w3.org/ns/oa#", "cnt"=>"http://www.w3.org/2011/content#", "dc"=>"http://purl.org/dc/elements/1.1/", "dcterms"=>"http://purl.org/dc/terms/", "dctypes"=>"http://purl.org/dc/dcmitype/", "foaf"=>"http://xmlns.com/foaf/0.1/", "rdf"=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdfs"=>"http://www.w3.org/2000/01/rdf-schema#", "skos"=>"http://www.w3.org/2004/02/skos/core#", "owl"=>"http://www.w3.org/2002/07/owl#", "prov"=>"http://www.w3.org/ns/prov#", "trig"=>"http://www.w3.org/2004/03/trix/rdfg-1/", "xsd"=>"http://www.w3.org/2001/XMLSchema#", "hasBody"=>{"@type"=>"@id", "@id"=>"oa:hasBody"}, "hasTarget"=>{"@type"=>"@id", "@id"=>"oa:hasTarget"}, "hasSource"=>{"@type"=>"@id", "@id"=>"oa:hasSource"}, "hasSelector"=>{"@type"=>"@id", "@id"=>"oa:hasSelector"}, "hasState"=>{"@type"=>"@id", "@id"=>"oa:hasState"}, "hasScope"=>{"@type"=>"@id", "@id"=>"oa:hasScope"}, "annotatedBy"=>{"@type"=>"@id", "@id"=>"oa:annotatedBy"}, "serializedBy"=>{"@type"=>"@id", "@id"=>"oa:serializedBy"}, "motivatedBy"=>{"@type"=>"@id", "@id"=>"oa:motivatedBy"}, "equivalentTo"=>{"@type"=>"@id", "@id"=>"oa:equivalentTo"}, "styledBy"=>{"@type"=>"@id", "@id"=>"oa:styledBy"}, "cachedSource"=>{"@type"=>"@id", "@id"=>"oa:cachedSource"}, "conformsTo"=>{"@type"=>"@id", "@id"=>"dcterms:conformsTo"}, "default"=>{"@type"=>"@id", "@id"=>"oa:default"}, "item"=>{"@type"=>"@id", "@id"=>"oa:item"}, "first"=>{"@type"=>"@id", "@id"=>"rdf:first"}, "rest"=>{"@type"=>"@id", "@id"=>"rdf:rest", "@container"=>"@list"}, "annotatedAt"=>{"@type"=>"xsd:dateTimeStamp", "@id"=>"oa:annotatedAt"}, "end"=>{"@type"=>"xsd:nonNegativeInteger", "@id"=>"oa:end"}, "exact"=>"oa:exact", "prefix"=>"oa:prefix", "serializedAt"=>{"@type"=>"xsd:dateTimeStamp", "@id"=>"oa:serializedAt"}, "start"=>{"@type"=>"xsd:nonNegativeInteger", "@id"=>"oa:start"}, "styleClass"=>"oa:styleClass", "suffix"=>"oa:suffix", "when"=>{"@type"=>"xsd:dateTimeStamp", "@id"=>"oa:when"}, "chars"=>"cnt:chars", "bytes"=>"cnt:bytes", "format"=>"dc:format", "language"=>"dc:language", "value"=>"rdf:value", "label"=>"rdfs:label", "name"=>"foaf:name", "mbox"=>"foaf:mbox"}, "@graph"=>[{"@id"=>"_:b92", "@type"=>"oa:Annotation", "hasBody"=>"_:b93", "hasTarget"=>"http://dms-data.stanford.edu/data/manifests/BnF/jr903ng8662/canvas/canvas-60#xywh=441,3599,1363,58", "motivatedBy"=>"sc:painting", "oa:serializedAt"=>{"@value"=>"2015-03-18T00:32:28Z", "@type"=>"xsd:dateTime"}, "serializedBy"=>"https://github.com/sul-dlss/annotations2triannon"}, {"@id"=>"_:b93", "@type"=>"cnt:ContentAsText", "chars"=>"Ne deffaites ou desdittes", "format"=>"text/plain", "language"=>"frm"}]}}
Unpermitted parameters: @context, @graph
Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method `save' for nil:NilClass):
  /data/src/dlss/ld4l_data/annotations/triannon/app/controllers/triannon/annotations_controller.rb:75:in `create'
  actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'

The POST was done with RestClient, using:

site = RestClient::Resource.new('http://localhost:3000', :user => '', :password => '')
response = site["/annotations/"].post oa_jsonld, :content_type => :json

where the oa_jsonld data is an open annotation with an OA context (in full, not as a URI), e.g.

{
  "@context": {
    "oa": "http://www.w3.org/ns/oa#",
    "cnt": "http://www.w3.org/2011/content#",
    "dc": "http://purl.org/dc/elements/1.1/",
    "dcterms": "http://purl.org/dc/terms/",
    "dctypes": "http://purl.org/dc/dcmitype/",
    "foaf": "http://xmlns.com/foaf/0.1/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
    "skos": "http://www.w3.org/2004/02/skos/core#",
    "owl": "http://www.w3.org/2002/07/owl#",
    "prov": "http://www.w3.org/ns/prov#",
    "trig": "http://www.w3.org/2004/03/trix/rdfg-1/",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "hasBody": {
      "@type": "@id",
      "@id": "oa:hasBody"
    },
    "hasTarget": {
      "@type": "@id",
      "@id": "oa:hasTarget"
    },
    "hasSource": {
      "@type": "@id",
      "@id": "oa:hasSource"
    },
    "hasSelector": {
      "@type": "@id",
      "@id": "oa:hasSelector"
    },
    "hasState": {
      "@type": "@id",
      "@id": "oa:hasState"
    },
    "hasScope": {
      "@type": "@id",
      "@id": "oa:hasScope"
    },
    "annotatedBy": {
      "@type": "@id",
      "@id": "oa:annotatedBy"
    },
    "serializedBy": {
      "@type": "@id",
      "@id": "oa:serializedBy"
    },
    "motivatedBy": {
      "@type": "@id",
      "@id": "oa:motivatedBy"
    },
    "equivalentTo": {
      "@type": "@id",
      "@id": "oa:equivalentTo"
    },
    "styledBy": {
      "@type": "@id",
      "@id": "oa:styledBy"
    },
    "cachedSource": {
      "@type": "@id",
      "@id": "oa:cachedSource"
    },
    "conformsTo": {
      "@type": "@id",
      "@id": "dcterms:conformsTo"
    },
    "default": {
      "@type": "@id",
      "@id": "oa:default"
    },
    "item": {
      "@type": "@id",
      "@id": "oa:item"
    },
    "first": {
      "@type": "@id",
      "@id": "rdf:first"
    },
    "rest": {
      "@type": "@id",
      "@id": "rdf:rest",
      "@container": "@list"
    },
    "annotatedAt": {
      "@type": "xsd:dateTimeStamp",
      "@id": "oa:annotatedAt"
    },
    "end": {
      "@type": "xsd:nonNegativeInteger",
      "@id": "oa:end"
    },
    "exact": "oa:exact",
    "prefix": "oa:prefix",
    "serializedAt": {
      "@type": "xsd:dateTimeStamp",
      "@id": "oa:serializedAt"
    },
    "start": {
      "@type": "xsd:nonNegativeInteger",
      "@id": "oa:start"
    },
    "styleClass": "oa:styleClass",
    "suffix": "oa:suffix",
    "when": {
      "@type": "xsd:dateTimeStamp",
      "@id": "oa:when"
    },
    "chars": "cnt:chars",
    "bytes": "cnt:bytes",
    "format": "dc:format",
    "language": "dc:language",
    "value": "rdf:value",
    "label": "rdfs:label",
    "name": "foaf:name",
    "mbox": "foaf:mbox"
  },
  "@graph": [
    {
      "@id": "_:b92",
      "@type": "oa:Annotation",
      "hasBody": "_:b93",
      "hasTarget": "http://dms-data.stanford.edu/data/manifests/BnF/jr903ng8662/canvas/canvas-60#xywh=441,3599,1363,58",
      "motivatedBy": "sc:painting",
      "oa:serializedAt": {
        "@value": "2015-03-18T00:41:41Z",
        "@type": "xsd:dateTime"
      },
      "serializedBy": "https://github.com/sul-dlss/annotations2triannon"
    },
    {
      "@id": "_:b93",
      "@type": "cnt:ContentAsText",
      "chars": "Ne deffaites ou desdittes",
      "format": "text/plain",
      "language": "frm"
    }
  ]
}

Without the OA context, the annotation has the following json representation:

{
  "@context": {
    "content": "http://www.w3.org/2011/content#",
    "dc11": "http://purl.org/dc/elements/1.1/",
    "oa": "http://www.w3.org/ns/oa#",
    "sc": "http://www.shared-canvas.org/ns/",
    "xsd": "http://www.w3.org/2001/XMLSchema#"
  },
  "@graph": [
    {
      "@id": "_:b92",
      "@type": "oa:Annotation",
      "oa:hasBody": {
        "@id": "_:b93"
      },
      "oa:hasTarget": {
        "@id": "http://dms-data.stanford.edu/data/manifests/BnF/jr903ng8662/canvas/canvas-60#xywh=441,3599,1363,58"
      },
      "oa:motivatedBy": {
        "@id": "sc:painting"
      },
      "oa:serializedAt": {
        "@value": "2015-03-18T00:42:21Z",
        "@type": "xsd:dateTime"
      },
      "oa:serializedBy": {
        "@id": "https://github.com/sul-dlss/annotations2triannon"
      }
    },
    {
      "@id": "_:b93",
      "@type": "content:ContentAsText",
      "content:chars": "Ne deffaites ou desdittes",
      "dc11:format": "text/plain",
      "dc11:language": "frm"
    }
  ]
}
dazza-codes commented 9 years ago

When the OA as jsonld is pasted into the form at /annotations/new, it works OK. But when RestClient does a POST, it fails. The RestClient POST looks like:

[13] pry(main)> tc = Annotations2triannon::TriannonClient.new
=> #<Annotations2triannon::TriannonClient:0x007fa19ca3a788
 @config={"host"=>"http://localhost:3000", "user"=>"", "pass"=>""},
 @site=#<RestClient::Resource:0x007fa19ca3a5a8 @block=nil, @options={:user=>"", :password=>""}, @url="http://localhost:3000">>
[14] pry(main)> tc.site["/annotations/"].post oa.to_jsonld_oa, :content_type => :json
RestClient::InternalServerError: 500 Internal Server Error
from /home/dlweber/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.7.3/lib/restclient/abstract_response.rb:48:in `return!'

The generic RestClient POST looks like (where oa.to_jsonld_oa data is as above):

 RestClient.post "http://localhost:3000/annotations/", oa.to_jsonld_oa, :content_type => :json, :accept => :json
RestClient::InternalServerError: 500 Internal Server Error
from /home/dlweber/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.7.3/lib/restclient/abstract_response.rb:48:in `return!'
dazza-codes commented 9 years ago

Passing this ticket to someone else, because my dev environment will not allow debugging with binding.pry in the post method.

dazza-codes commented 9 years ago

I got pry working, only by adding 'pry' to the standard gem dependencies (outside the development group). The dependency management for a rails engine is a bit different from an app; see http://edgeguides.rubyonrails.org/engines.html#other-gem-dependencies

This guide documentation indicates that development deps for an engine are only loaded when using the test app; as they say, "The development dependencies for the gem will only be used when the tests for the engine are running." So, when I run rake triannon:server_jetty it never loads pry when it is a dev dependency.

dazza-codes commented 9 years ago

This was solved by wrapping the post data with additional params, i.e.

post_data = {
        "commit" => "Create Annotation",
        "annotation" => { "data" => oa.to_jsonld_oa }
      }