What's the right way to use custom exceptions in Restler? - php

Lets say insted of just output:
{
"error":
{
"code": 500,
"message": "Some internal error"
}
}
I would like to output:
{
"error":
{
"code": 500,
"message": "Some internal error",
"error_code" : 1050
}
}
Also is there a way we can catch all the exceptions for log purposes for example?

Use RestException to throw the exception and use the details parameter (an array) to add additional details
throw new RestException(400, 'invalid user', array('error_code' => 12002));
gives me the following
{
"error": {
"code": 400,
"message": "Bad Request: invalid user",
"error_code": 12002
},
"debug": {
"source": "Say.php:5 at call stage",
"stages": {
"success": [
"get",
"route",
"negotiate",
"validate"
],
"failure": [
"call",
"message"
]
}
}
}
Info:- additional debug information is returned when restler is running in debug mode.
It can be turned off by using Compose::$includeDebugInfo=false;
Note:- Make sure you are using Restler 3.0 RC4 or higher

Related

Catch request errors openapi-validation-middleware and change jsooutput

I use this repository hkarlstrom/openapi-validation-middleware to verify incoming and outgoing json requests and responses in my api build on php slim.
I want to rework the error output to another json format.
Currently it is, as example:
Error 400:
{
"message": "Request validation failed",
"errors": [
{
"name": "catalogus",
"code": "error_format",
"value": "",
"in": "query",
"type": "string",
"format": "uri"
}
]
}
I tried to catch this error so i can modify the repsonse like this:
$mw = new HKarlstrom\Middleware\OpenApiValidation(__DIR__.'/catalog_openapi.json');
$options = [
'validateRequest'=> false,
'exampleResponse' => true,
'beforeHandler' => function (\Psr\Http\Message\ServerRequestInterface $request, array $errors) : \Psr\Http\Message\ServerRequestInterface {
// Alter request modifiy the json reponse here !!!
echo "tadaaaa"; <- never outputted
return $request;
}
];
And finally in my route:
$app->get('/v1/dosomething[/{params:.*}]',\App\Action\DoSomething::class)->add($mw,$options);
But it never touches the before- or errorHandler.
Any ideas how to get this working?

Google ads API "PERMISSION_DENIED"

I get the error :
{
"message": "The caller does not have permission",
"code": 7,
"status": "PERMISSION_DENIED",
"details": [
{
"#type": "type.googleapis.com\/google.ads.googleads.v10.errors.GoogleAdsFailure",
"errors": [
{
"errorCode": {
"authorizationError": "USER_PERMISSION_DENIED"
},
"message": "User doesn't have permission to access customer. Note: If you're accessing a client customer, the manager's customer id must be set in the 'login-customer-id' header. See https:\/\/developers.google.com\/google-ads\/api\/docs\/concepts\/call-structure#cid"
}
],
"requestId": "asds33ad3sdadad334"
}
]
}
and the code I get from here : https://developers.google.com/google-ads/api/docs/reporting/example
what I need to do with login-customer-id ?

can I throw custom error in laravel-lighthouse

is the any way that throw an error by GraphQL\Error\Error with no additional data except message.
the current return data is
{
"errors": [
{
"message": "Some Errors",
"extensions": {
"reason": "",
"category": "custom"
},
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"to the path"
],
"trace": [{},{},{},{}]
}
],
"data": {
"testQuery": null
}
}
it contains unnecessary data
but I want something like:
{
"errors": [
{
"message": "Some Errors",
"extensions": {
"reason": "",
"category": "custom"
},
],
"data": {
"testQuery": null
}
}
You can throw your own exceptions, that will allow you to do that. Check https://lighthouse-php.com/5/digging-deeper/error-handling.html#registering-error-handlers.
for a simple error you can use this code, in case you don't want to create an error handler class
use GraphQL\Error\Error;
...
return Error::createLocatedError('Some Errors');

"URL Can't Be Used" error in adding facebook post with child_attachments via api

i'm trying to make a post on facebook using api with using child_attachments param, so i can pass multiple urls in 1 post.
In facebook debugger i'm passing json like that
{
"message": "test debug",
"child_attachments": [
{
"link": {
"link": "https://google.com"
}
},
{
"link": {
"link": "https://ft.com"
}
}
]
}
but i'm getting following error message
{ "error": {
"message": "Invalid parameter",
"type": "OAuthException",
"code": 100,
"error_data": {
"blame_field_specs": [
[
"child_attachments[0][link]"
]
]
},
"error_subcode": 2061006,
"is_transient": false,
"error_user_title": "URL Can't Be Used",
"error_user_msg": "The URL you entered, \"https://google.com\" doesn't direct to a website. Please enter a valid URL and try again.",
"fbtrace_id": "xxxxxxxx"
}
}
i've tried setting urls in app domains , i tried use urls with / without https and with/without www.
still no luck, can some one give me some advise?
Thanks,
Roman.
ok, so i figured, the correct format should be like that:
{
"message": "message to post",
"link": "https://yahoo.com",
"child_attachments": [
{
"link": "https://yahoo.com",
"name": "name -1",
"picture": "https://images.all-free-download.com/images/graphiclarge/ethereal_image_185195.jpg"
},
{
"link": "https://google.com",
"name": "name - 2 ",
"picture": "https://images.all-free-download.com/images/graphiclarge/ethereal_image_185195.jpg"
}
]
}
note that even despite of the thing you adding links in the child_attachments, you must provide 'link' parameter also for post

How can I get the data from my bookshelf for Google Books API

I been trying to use Google Books to request the data from my bookshelf and use it in my page. When I use the sample page list and try it it give me the results that I want, showing something like the following
{
"kind": "books#volumes",
"totalItems": 803,
"items": [
{
"kind": "books#volume",
.
.
.
which I know how use. However when I try to use use it a GET request using the sample that it gives me GET https://www.googleapis.com/books/v1/users/myUSERID/bookshelves/1001/volumes in PHP it always give me the following error page
{
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "The bookshelf ID could not be found.",
"locationType": "other",
"location": "bookshelf_id,bookshelf_owner_user_id"
}
],
"code": 404,
"message": "The bookshelf ID could not be found."
}
}
How can I do the request through the API? I am new to using APIs and I want to use the more direct approach.

Categories