Generic custom error response in Dingo API - lumen

I am trying to abstract all my error responses to match my API documentation for developing a good, pragmatic API using Lumen and the Dingo API package:
A JSON error body should provide a few things for the developer - a useful error message, a unique error code (that can be looked up for more details in the docs) and possibly a detailed description. JSON output representation for something like this would look like:
{
"code" : 1234,
"message" : "Something bad happened :(",
"description" : "More details about the error here"
}
At the moment, the Dingo API only provides me with the following response:
{
"message": "Could not create new user.",
"status_code": 422
}
1. How can I add extra fields to all error responses, e.g "description" and "code"
The configuration page shows a possible error format to set on your bootstrap/app.php file but I'm wondering how I can add possible new tags and use these.
I know it's possible using a custom exception and returning a new response:
app('Dingo\Api\Exception\Handler')->register(function (\App\Exceptions\ValidationException $exception) {
return Response::make(['code' => $exception->getCode(), 'message' => $exception->getMessage()], 401);
});
I want to know if this is the best practice to do it like this but on all exceptions (including the default ones of Dingo/Symfony), for example using the configuration parameters: ->setErrorFormat([]);
2. Change validation errors format
Validation errors for PUT, PATCH and POST requests will need a field breakdown. This is best modeled by using a fixed top-level error code for validation failures and providing the detailed errors in an additional errors field, like so:
{
"code" : 1024,
"message" : "Validation Failed",
"errors" : [
{
"code" : 5432,
"field" : "first_name",
"message" : "First name cannot have fancy characters"
},
{
"code" : 5622,
"field" : "password",
"message" : "Password cannot be blank"
}
]
}
Currently, Dingo API provides me the JSON in the following format:
{
"message": "Could not create new user.",
"status_code": 422,
"errors": {
"username": [
"The username field is required."
],
"password": [
"The password field is required."
]
}
}

Related

Can't create In-App Purchase Review Screenshot by API

I am trying to create an In-App Purchase Review Screenshot to create a new In-App Purchase by API.
I'm following Apple's guide, but am stuck at step : "Make an image reservation with POST /v1/inAppPurchaseAppStoreReviewScreenshots (Create an In-App Purchase Review Screenshot)."
Payload example :
{
"data": {
"type": "inAppPurchaseAppStoreReviewScreenshots",
"attributes": {
"fileName": "my-screenshot.jpg",
"fileSize": "45482"
},
"relationships": {
"inAppPurchaseV2": {
"data": {
"id": "1651010682",
"type": "inAppPurchases"
}
}
}
}
}
I always get error 409.
Exception = Client error: `POST https://api.appstoreconnect.apple.com/v1/inAppPurchaseAppStoreReviewScreenshots` resulted in a `409 Conflict` response:
{
"errors" : [ {
"id" : "7c813a34-37a6-49e7-be5f-699236659d0e",
"status" : "409",
"code" : "ENTITY_ERROR.A (truncated...)
I tried to remove V2 from inAppPurchaseV2 in relationships, but get the same error.
Has anyone succeeded by any chance?
I found the cause of the problem, it's because I was passing a string on "fileSize". By removing the quotes everything works!

How to create a BigQuery View using REST request

I've tried building and sending the REST request using the Google Tables: insert page with the following (sanitised) values:
Request parameters
projectId:prj-name
datasetId:dataset_name
Request body
{
"view": {
"useLegacySql": false,
"query": "SELECT * FROM `prj-name.dataset_name.hello_world`"
},
"type": "VIEW",
"tableReference": {
"projectId": "prj-name",
"datasetId": "dataset_name",
"tableId": "v_hello_world"
}
}
This post suggests these parameters should work, but Google returns the following response:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "Output field used as input"
}
],
"code": 400,
"message": "Output field used as input"
}
}
I've experimented with the REST properties and I think the 400 issue is caused by the inclusion of the tableReference property. But if I exclude it I get the following alternative error:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Required parameter is missing"
}
],
"code": 400,
"message": "Required parameter is missing"
}
}
It's a shame that the "Required parameter" is not named in this error message!
I can use this code method to create the view but that code method throws an exception when patching views.
Help is appreciated. I'd rather not have to maintain two different ways of managing views in my code.
Thanks.
Remove "type": "VIEW", from your request.
That's not an input parameter. It's an output field from the response.
https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#resource

Paypal API - checkout.js - parse response error

I've implemented this API on my Angular app. The issue is when I'm entering wrong shipping address the API is throwing the error:
Uncaught Error: An error was encountered while handling error
Error: Request to post www.sandbox.paypal.com/v1/payments/payment failed with 400 error. Correlation id: 3b331f9eaafa4, 3b331f9eaafa4
{
"name": "VALIDATION_ERROR",
"details": [
{
"field": "city",
"issue": "Value is invalid"
},
{
"field": "state",
"issue": "Value is invalid"
},
{
"field": "zip",
"issue": "Value is invalid"
}
],
"message": "Invalid request - see details",
"information_link": "developer.paypal.com/docs/api/payments/#errors",
"debug_id": "3b331f9eaafa4"
}
How do I parse the error in order to show a alert with the error's text? It's not JSON, so I don't know how to have it parsed.
This is the solution:
let res = error.message.match(/{([\s\S]*?)^}/gm);
console.log( JSON.parse( res[0] ) )
This will give a JSON to work with.
Use regex to match the JSON and then parse it to JSON using JSON.parse. Once done you can use the message and details fields which contain the information related to the error.
var jsonString = errmsg.match(/\{.*\}/gm);
var jsonObj = JSON.parse(jsonString);
Hope this helps.

firebase - Firestore REST API starting query

Hello I am new to firestore and I'm trying to use the rest API to query from a collection called Users, find all users who have a field called about with the value test2
This is my POST request :
https://firestore.googleapis.com/v1beta1/projects/{myprojectid}/databases/(default)/documents/Users:runQuery
Body:
{
"structuredQuery": {
"where" : {
"fieldFilter" : {
"field": {"fieldPath": "about"},
"op":"EQUAL",
"value": {"string": "test2"}
}
},
"from": [{"collectionId": "Users"}]
}
}
I get a response
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"structured_query\" at 'document': Cannot find field.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "document",
"description": "Invalid JSON payload received. Unknown name \"structured_query\" at 'document': Cannot find field."
}
]
}
]
}
}
Can someone tell me what I am doing wrong ? Thanks a lot. I'm stuck and unable to proceed.
In your URL get rid of the Users.
https://firestore.googleapis.com/v1beta1/projects/{myprojectid}/databases/(default)/documents:runQuery
Also, use stringValue instead of string value type. https://cloud.google.com/firestore/docs/reference/rest/v1beta1/Value
Just remember that you need to convert the url and take care with the version v1beta1 or v1, so in final form it will be like this:
https://firestore.googleapis.com/v1/projects/{myprojectid}/databases/%28default%29/documents:runQuery

GoodData "Create Report Definition" API Call giving 500 Internal Server Error

I'm trying to create a report definition using the GoodData REST API. I use the following endpoint to invoke the rest call.
"/gdc/md/{project-id}/obj"
When i try to invoke the API call with the following dataset in which the projectId and the userId are valid, it gives me the error with the response code 500.
{
"reportDefinition": {
"content": {
"filters": [],
"format": "grid",
"grid": {
"rows": [],
"columns": [
"metricGroup"
],
"sort": {
"columns": [],
"rows": []
},
"columnWidths": [],
"metrics": [
{
"uri": "/gdc/md/qy48iv4flikdlcwpwioizuip74wt8nb5/obj/63f3cecd2a8d3ce2ec9378381c8f39e3",
"alias": ""
}
]
}
},
"meta": {
"title": "Sample report definition",
"summary": "This is a sample report",
"tags": "",
"deprecated": 0,
"category": "samplecategory"
}
}
}
{
"error": {
"message": "Internal server error. Please fill in bug report with request_id='lp78FL5S1IPMqB2n'"
}
}
I'm certain that the user project_id and the user_id are valid. Is this an error in the API?
Thank you in advance.
Apart from the metrics URI that looks weird (hash instead of numeric ID), I was able to dig in our logs an error that says: "Category is not equal to tag structure".
In your example you have its value set to "samplecategory". "category" property defines what type of object are you creating. If you are creating a report definition it should have value of "reportDefinition".
Last time I worked with GoodData API, metrics had numeric IDs. That seems most likely to be the culprit. Where did you get "/gdc/md/qy48iv4flikdlcwpwioizuip74wt8nb5/obj/63f3cecd2a8d3ce2ec9378381c8f39e3" from, especially the "63f3cecd2a8d3ce2ec9378381c8f39e3" part?