How to handle json API response errors in dart? - json

I was trying to create a library to parse a few JSON http responses from an API so I made the request and created a model class and parsed the JSON response but sometimes (maybe if the user token expires), an exception with error 401 is returned, and since the error id is required to handle different errors I was thinking of creating another model class for the error and returning that to the user, but that would mean I'll have to either return the class with the required data or the class with the error information which is not possible to do with the same function at a time. Now, if I did a try-catch clause it wouldn't return anything if an exception occurs returning null (which I don't want).
The correct JSON response looks like the following:
{
"result": "ok",
"token": {
"session": "string",
"refresh": "string"
}
}
and the error JSON response will look like the following:
{
"result": "error",
"errors": [
{
"id": "string",
"status": 0,
"title": "string",
"detail": "string"
}
]
}
Now, of course I could do an if else clause checking the response code and doing the rest but again I'll somehow have to return the error class with all the detail in it. How do I do so?
Note: I'm using the mangadex api

I'd make a function that returns Future<Token>
If the result is okay, parse Token and return it.
If it's an error, throw a custom Error implementation with all of the details.
Then you create an idiomatic Dart API for your RPC.

Related

Step Functions: Passing in json to RequestBody to API Gateway via POST method

so I have a json that is being received by another API Gateway that is invoked.
I want to pass this json in another Task that invokes another API Gateway. I tried to include it in the RequestBody via the $ identifier but it literally sends this without the JSON. Attempting to add ResultPath or InputPath on this Task throws error.
{
"ApiEndpoint": "fasdffasd.execute-api.us-east-1.amazonaws.com",
"Method": "POST",
"Headers": {
"Content-Type": [
"application/json"
]
},
"Stage": "uat",
"Path": "/v1/order/create",
"RequestBody": {
"Payload": "$" <---the JSON received by this Task from the previous Task
},
"AuthType": "IAM_ROLE"
}
The issue is checking CloudWatch Logs I can see that literally the dollar sign as a string is returned. I expected a JSON object.

Wrong example response being sent with modified request body

I have a single request "auth" that simply sends the below raw data..
{
"password": "123",
"userName": "rob"
}
and i have setup two examples. The first called "success" takes the above request and returns..
{
"user": {
"profile": 1234
}
}
I then wanted to test scenario whereby the password (and eventually user name) is empty so i can see error being returned. So a second example "No Password" sends
{
"password": "",
"userName": "rob"
}
and the response is
{
"id": {
"code": 1
},
"errorObject": [
{
"errorCategory": "INPUT_ERROR",
"errorName": "PWRD_EMPTY",
}
]
}
Now the issue is that regardless of what request i make i always get back the error response. I've tested this a few times and i think it's actually just returning whatever the newest example created was.
Is Postman able to detect differences in json data in the request body? I have tried creating a second "auth" request, one for success scenario and the other for the failure but this didn't resolve the issue.

POSTMAN How to parse a nested json object in postman which has dynamic keys?

supposing the json body returned from a call contains some dynamic keys ie
{
"message": "search results matching criteria",
"permission": {
"261ef70e-0a95-4967-b078-81e657e32699": {
"device": {
"read:own": [
"*"
]
},
"account": {
"read:own": [
"*"
]
},
"user": {
"read:own": [
"*"
]
}
}
}
I can validate the json as follows easily enough although I am having a lot of trouble working out how to validate the objects BELOW the dynamic guid level of the response.
pm.test("response body to have correct items", function () {
pm.expect(jsonData.message).to.eq("search results matching criteria");
pm.expect(jsonData).to.have.property('permission');
pm.expect(jsonData.permission).to.have.property(pm.variables.get("otherUserId"));
});
Would ideally like to verify the device and account and user levels of the object.
Anyone with some tips?
I've tried a few ways to try and reference the otherUserId variable but nothing is working. It is either not resolving the variable therefore failing the test as its looking for a level in the json called otherUserId or it fails to run the test due to a syntax error.
This works:
pm.expect(jsonData.permission[pm.variables.get("otherUserId")]).to.have.property('device');

JSON Schema Validation in Mule: get failing field

I am using APIkit in Mule with RAML 0.8 and a JSON schema, as follows (example):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"cart": {
"title": "",
"description": "",
"type": "object",
"properties": {
"internalNumber": {
"type": "integer"
}
},
"required": [
"internalNumber"
]
}
},
"required": [
"cart"
]
}
and in the Mule Flow, I catch the exception and show the following result:
#[exception.cause.message]
When a validation error occurs, I want to get the name of the field in which the validation failed. Instead, this is what I got:
Input
{
"cart": {
"internalNumber": "I must be an integer"
}
}
Output
"instance type (string) does not match any allowed primitive type (allowed: ["integer"])"
Expected output
{
"field": "cart.internalNumber",
"error": "instance type (string) does not match any allowed primitive type (allowed: ["integer"])"
}
All I want to know is if there is a way to get the name of the field in which the validation errors occurs.
Regarding the Mule Documentation, I can get the whole JSON string but not the name of the failing field...
I hope someone can give me a better solution.
Thanks!
Within your JSON Schema, add "required":"true" attribute, to make the fields mandatory.
You can also use JSON schema validator, in your mule flow, by referring to the updated schema.
Any of the case should through you an error with missing field.
Use below expression to get expected error message.
{
"errorMessage": "#[exception].toString().replace("\"","\\\"")"
}
Not sure if you are expecting it as an output or looking for a way to validate your input and schema.
I can try to suggest on "All I want to know is if there is a way to get the name of the field in wich the validation errors occurs."; to do this better validate your JSON and input data through online validator before defining definitions. Like using http://www.jsonschemavalidator.net/, it will help you with error and fields. Hope this may help!

How to get the full pointer data in Parse.com

i'm making a new application using Parse.com as a backend, i'm trying to make less requests to the Parse, I have a class which is pointing to another object of another class.
Class1(things):
ObjectID Name Category(pointer)
JDFHSJFxv Apple QSGKqf343
Class2(Categories):
ObjectID Name Number Image
QSGKqf343 Fruits 45 http://myserver.com/fruits.jpeg
when i'm trying to retreive data for my first class things using REST API i'm getting this json object :
{
"results": [
{
"Name": "Apple",
"createdAt": "2015-07-12T02:50:20.291Z",
"objectId": "JDFHSJFxv",
"category": {
"__type": "Pointer",
"className": "Teams",
"objectId": "QSGKqf343"
},
"updatedAt": "2015-07-12T02:55:33.696Z"
}
]
}
the json doesn't contains all the data included in the object i'm pointing to, I will have to make another request to get all the data of that object,
is There any way to fix that
You need to tell Parse to return the related object in your query, via the include key.
e.g., add the following to your CURL --data-urlencode 'include=category'