Display JSON Api conform errors - django

I receive JSON Api conform errors from the backend:
{
"errors": [
{
"status": "400",
"source": {
"pointer": "/data/attributes/description"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/due-date"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/extra-comments"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/name"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/payment-type"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/price"
},
"detail": "This field may not be null."
}
]
}
I try to show them in my template, as described in the EmberData documentation:
{{#each model.errors.messages as |message|}}
<div class="error">
{{message}}
</div>
{{/each}}
Nothing is shown. I would say the .errors in the model are not populated, but I am not sure how to check this. How can I:
display the received ajax reply?
make sure that EmberData is processing the reply and populating model.errors?
Show the processed model.errors in the console?
Show the model and all properties?
In general, I am experiencing that new versions of Ember are very hard to debug. Whenever I show any Ember object in the console, I just see some Computed properties which are not expanded whenever I try to peek into them.
My backend is:
Django 1.9
with django-rest-framework
django-rest-framework-json-api
EDIT
This is the data that I am POSTing to the backend (JSONAPi conform):
{
"data": {
"attributes": {
"name": null,
"description": null,
"extra-comments": null,
"min-price": 30,
"max-price": 3000,
"price-step": 10,
"price": null,
"payment-type": null,
"due-date": null
},
"relationships": {
"seller": {
"data": null
},
"artist": {
"data": null
},
"subcategory": {
"data": null
}
},
"type": "projects"
}
}
The backend is ok with this, detects the errors, and provides a JSON APi conform errors reply, as specified above.

I think I know what's happening (as it happened to me as well).
Change the HTTP error code from 400 to 422 (Unprocessable Entity) and check if it fixes the problem.
Also, looking at the source code for the JSONAPIAdapter (which extends from the RestAdapter) I think I'm right.
isInvalid: function(status, headers, payload) {
return status === 422;
},
This can be changed to (adapters/application.js):
import DS from 'ember-data';
import config from '../config/environment';
export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
host: config.API_HOST,
namespace: config.API_NAMESPACE,
isInvalid: function(status, headers, payload) {
return status === 400 || status === 422;
},
});

For reference, I have done this on the django side:
from rest_framework_json_api.exceptions import exception_handler
def custom_exception_handler(exc, context):
# DRF returns 400, but EmberData wants 422. I will force a 422, always.
# Call the rest_framework_json_api's exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
# TODO: is this correct? 422 in all exception cases?!
response.status_code = 422
return response

Related

AWS LexV2 messenger channel integration not sending requestAttributes

I am building a AWS LexV2 chat bot that I want to integrate with Facebook Messenger via Channel Integration provided by LexV2. I am also using Lambda codehook to validate my inputs and fulfil my intents.
Following the docs, everything works as expected, except for one thing. When I log the event in my lambda function, requestAttributes field is missing from the object
My lambda function code:
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
def lambda_handler(event, context):
logger.error(event)
return ...
When I send a message to my app/page on facebook, this is what is logged:
{
"sessionId": "session-id",
"inputTranscript": "Intent utterance",
"interpretations":
[
{
"intent":
{
"slots":
{
"slot1": null,
"slot2": null
},
"confirmationState": "null",
"name": "IntentName",
"state": "InProgress"
},
"nluConfidence": 1.0
},
{
"intent":
{
"slots":
{},
"confirmationState": "null",
"name": "FallbackIntent",
"state": "InProgress"
}
}
],
"responseContentType": "text/plain; charset=utf-8",
"invocationSource": "DialogCodeHook",
"messageVersion": "1.0",
"sessionState":
{
"intent":
{
"slots":
{
"slot1": null,
"slot2": null
},
"confirmationState": "null",
"name": "IntentName",
"state": "InProgress"
},
"originatingRequestId": "req-id"
},
"bot":
{
"aliasId": "ALIASID",
"aliasName": "AliasName",
"name": "BotName",
"version": "DRAFT",
"localeId": "en_US",
"id": "bot-id"
},
"inputMode": "Text"
}
As you can see, no requestAttributes.
What's weird to me is that when I do the same thing for LexV1 bot (same facebook page/messenger app), I get these fields, e.g.
{
"requestAttributes":
{
"x-amz-lex:facebook-page-id": "page-id",
"x-amz-lex:channel-id": "channel-id",
"x-amz-lex:webhook-endpoint-url": "webhook-endpoint",
"x-amz-lex:accept-content-types": "PlainText",
"x-amz-lex:user-id": "user-id",
"x-amz-lex:channel-name": "channel-name",
"x-amz-lex:channel-type": "Facebook"
}
}
If anyone has any tips (other than "switch to V1") I would be very grateful. Thanks :))
Notes:
Changed Python Nones to nulls to easier format JSON
Replaced IDs and names from the logs with generic-id-slash-names

Django data handling in custom middleware

In my django webapp, I have to pass user data param(udp) in a request and return the same in response without modifying it in view.
Request
{
"mobile": "111111111",
"udp":[
{
"key": "keyName",
"value":"valueName"
},
{
"key": "keyName",
"value":"valueName"
},
]
}
Response
{
"code": "200",
"message": "success message",
"response": {
"data":"user data"
"udp":[
{
"key": "keyName",
"value":"valueName"
},
{
"key": "keyName",
"value":"valueName"
}
]
}
}
I thought of acheiving this by writting custom middleware but after accessing request in middleware, View is throwing me error
you cannot access body after reading from request's data stream
Can anyone suggest how this can be implemented?
or
What will be best approach of doing this in django

Autodesk BIM360: Folder Creation returns 400 Bad request

I called the POST-Folder Method via Postman with a JSON body according to this example. But I only receive the message "400 Bad Request" without any explanations. This is what my request looks like:
The service adress:
https://developer.api.autodesk.com/data/v1/projects/b.5823d0b2-0000-0000-00/commands
The HTTP-header
Authorization: Bearer 2_legged_token
Content-Type: application/vnd.api+json
The JSON-Body
{
"jsonapi": {
"version": "1.0"
},
"data": {
"type": "commands",
"attributes": {
"extension": {
"type": "commands:autodesk.core:CreateFolder",
"version": "1.0.0",
"data": {
"requiredAction": "create"
}
}
},
"relationships": {
"resources": {
"data": [
{
"type": "folders",
"id": "1"
}
]
}
}
},
"included": [
{
"type": "folders",
"id": "1",
"attributes": {
"name": "test",
"extension": {
"type": "folders:autodesk.bim360:Folder",
"version": "1.0.0"
}
},
"relationships": {
"parent": {
"data": {
"type": "folders",
"id": "urn:adsk.wipprod:fs.folder:co.Ai*****"
}
}
}
}
]
}
The response
{
"jsonapi": {
"version": "1.0"
},
"errors": [
{
"id": "f1266e76-a37e-400b-bff6-de84b11cdb00",
"status": "400",
"detail": "BadRequest"
}
]
}
What I have found out so far:
The project id is right. When I take a wrong project id I receive a different error.
The Json is also valid.
When I take a (surely) wrong parent-folder-urn I'll receive the same error message. So maybe this is a wrong urn format or something?
As of now, you can create a BIM 360 Docs Folder with commands endpoints, as you pointed out. For that you can use:
3-legged token
2-legged token with x-user-id header, this should contain the Autodesk User ID obtained, for instance, from GET users#me endpoint
"pure" 2-legged token will return bad request (as of August/2017)
Sorry about the documentation, the endpoint to create BIM 360 Docs folder via Commands was released a couple weeks back and we're just finishing to write the documentation.

How to get included values of jsonapi on a router with ember-data?

I'm using jsonapi directives to make a connection between ember.js app and an API. I have a track that could have like 50 or more comments and I need to load them on a route to perform some logic.
This is and example response of the API:
{
"data": {
"id": 1,
"type": "track",
"attributes": {
"name": "XPTO"
},
"relationships": {
"comment": {
"data": [
{"id": 1, "type": "comment"}
]
}
}
},
"include": [
{
"id": 1,
"type": "comment",
"attributes": {
"text": "Lorem ipsum..."
}
}
]
}
Now imagine it with 50 comments, this would be very consuming to make a request for each call. If I do it in a view with an each loop, it doesn't make all the requests, but it I try to access it in a Route, it will make all the requests. How do I achieve that with the following code?
this.store.findRecord('track', 1).then((t) => {
// logic here
// I tried this but it would make all the requests too
t.get('comments');
})
U can use property "coalesceFindRequests" in your adapter
coalesceFindRequests: true
If you set coalesceFindRequests to true it will instead trigger the following request:
GET /comments?ids[]=1&ids[]=2
so this makes only one call for all the comments.
Note: Requests coalescing rely on URL building strategy. So if you
override buildURL in your app groupRecordsForFindMany more likely
should be overridden as well in order for coalescing to work.

Handling Errors with RESTSerializer

Using ember and ember-data 2.6 versions, I'm trying to get error handling working but running into issues. I'm using the RESTSerializer and I'm sending the following payload to my server:
{
"brand": {
"name": null,
"description": null,
}
}]
Since name cannot be null, this is what I'm sending back as my response:
{
"errors": [{
"code": null,
"detail": "may not be null",
"status": null,
"title": null,
"source": {
"pointer": "brand/name",
"parameter": null
}
}]
}
In my route, I am doing the following in the save promise on failure:
console.log(savedBrand.get('isValid')); // logs false
console.log(savedBrand.get('errors').toArray()); // logs [ ]
There doesn't seem to be a lot of detailed documentation on how this all works so I'm using this article as a starting point. I'm unsure as to why toArray() returns an empty array instead of my error that I'm returning in the JSON.
even though I'm not passing in the data per the json-api, the pointer still had to be set as if I were:
{
"errors": [{
"code": null,
"detail": "may not be null",
"status": null,
"title": null,
"source": {
"pointer": "/data/attributes/name",
"parameter": null
}
}]
}