Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed 500 error when requesting include that didn't exist #1138

Merged
merged 6 commits into from
Feb 17, 2019

Conversation

cclep
Copy link
Contributor

@cclep cclep commented Aug 2, 2016

  • Error was caused by the eager loading functionality
  • Fix was to check for available includes if possible before blindly using them as an eager load
  • This fix will quietly ignore invalid includes. We should consider throwing a http exception as a better fix

cclep added 3 commits August 2, 2016 11:42
 - Error was caused by the eager loading functionality
 - Fix was to check for available includes if possible before blindly using them as an eager load
@trycatchjames
Copy link
Contributor

Bump. @hskrasek can this get reviewed please? Thanks mate

@hskrasek
Copy link
Member

Is there an issue or something that this references? Not sure I fully understand the issue thats being fixed here

@cclep
Copy link
Contributor Author

cclep commented Aug 29, 2016

a 500 error was thrown if an include not present in the 'available includes' is requested. This fixes this error by checking first if the include is valid.

@hskrasek
Copy link
Member

@cclep Do you have an example of that 500 error?

@cclep
Copy link
Contributor Author

cclep commented Aug 30, 2016

@hskrasek when requesting ?include=doesntexist
{
"message": "Call to undefined method Illuminate\Database\Query\Builder::doesntexist()",
"statusCode": 500,
"debug": {
"line": 2101,
"file": "/home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php",
"class": "BadMethodCallException",
"trace": [
"#0 [internal function]: Illuminate\Database\Query\Builder->__call('doesntexist', Array)",
"#1 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1015): call_user_func_array(Array, Array)",
"#2 [internal function]: Illuminate\Database\Eloquent\Builder->__call('doesntexist', Array)",
"#3 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(3444): call_user_func_array(Array, Array)",
"#4 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(545): Illuminate\Database\Eloquent\Model->__call('doesntexist', Array)",
"#5 [internal function]: Illuminate\Database\Eloquent\Builder->Illuminate\Database\Eloquent{closure}()",
"#6 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php(171): call_user_func(Object(Closure))",
"#7 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(546): Illuminate\Database\Eloquent\Relations\Relation::noConstraints(Object(Closure))",
"#8 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(517): Illuminate\Database\Eloquent\Builder->getRelation('doesntexist')",
"#9 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(497): Illuminate\Database\Eloquent\Builder->loadRelation(Array, 'doesntexist', Object(Closure))",
"#10 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php(43): Illuminate\Database\Eloquent\Builder->eagerLoadRelations(Array)",
"#11 [internal function]: Illuminate\Database\Eloquent\Collection->load(Array)",
"#12 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Pagination/AbstractPaginator.php(475): call_user_func_array(Array, Array)",
"#13 /home/vagrant/platform3/backend/vendor/dingo/api/src/Transformer/Adapter/Fractal.php(98): Illuminate\Pagination\AbstractPaginator->__call('load', Array)",
"#14 /home/vagrant/platform3/backend/vendor/dingo/api/src/Transformer/Factory.php(87): Dingo\Api\Transformer\Adapter\Fractal->transform(Object(Illuminate\Pagination\LengthAwarePaginator), Object(Platform\API\V1\Transformers\TargetedCampaign\ChangeAgentTransformer), Object(Dingo\Api\Transformer\Binding), Object(Dingo\Api\Http\Request))",
"#15 /home/vagrant/platform3/backend/vendor/dingo/api/src/Http/Response.php(119): Dingo\Api\Transformer\Factory->transform(Object(Illuminate\Pagination\LengthAwarePaginator))",
"#16 /home/vagrant/platform3/backend/vendor/dingo/api/src/Routing/Router.php(603): Dingo\Api\Http\Response->morph('json')",
"#17 /home/vagrant/platform3/backend/vendor/dingo/api/src/Routing/Router.php(572): Dingo\Api\Routing\Router->prepareResponse(Object(Dingo\Api\Http\Response), Object(Dingo\Api\Http\Request), 'json')",
"#18 /home/vagrant/platform3/backend/vendor/dingo/api/src/Http/Middleware/Request.php(112): Dingo\Api\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))",
"#19 [internal function]: Dingo\Api\Http\Middleware\Request->Dingo\Api\Http\Middleware{closure}(Object(Dingo\Api\Http\Request))",
"#20 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func(Object(Closure), Object(Dingo\Api\Http\Request))",
"#21 /home/vagrant/platform3/backend/vendor/barryvdh/laravel-cors/src/HandlePreflight.php(25): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Dingo\Api\Http\Request))",
"#22 [internal function]: Barryvdh\Cors\HandlePreflight->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#23 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)",
"#24 /home/vagrant/platform3/backend/vendor/barryvdh/laravel-cors/src/HandleCors.php(34): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Dingo\Api\Http\Request))",
"#25 [internal function]: Barryvdh\Cors\HandleCors->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#26 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)",
"#27 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(44): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Dingo\Api\Http\Request))",
"#28 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#29 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)",
"#30 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Dingo\Api\Http\Request))",
"#31 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func(Object(Closure), Object(Dingo\Api\Http\Request))",
"#32 /home/vagrant/platform3/backend/vendor/dingo/api/src/Http/Middleware/Request.php(113): Illuminate\Pipeline\Pipeline->then(Object(Closure))",
"#33 /home/vagrant/platform3/backend/vendor/dingo/api/src/Http/Middleware/Request.php(89): Dingo\Api\Http\Middleware\Request->sendRequestThroughRouter(Object(Dingo\Api\Http\Request))",
"#34 [internal function]: Dingo\Api\Http\Middleware\Request->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#35 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)",
"#36 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request))",
"#37 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\Http\Request))",
"#38 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(122): Illuminate\Pipeline\Pipeline->then(Object(Closure))",
"#39 /home/vagrant/platform3/backend/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(87): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))",
"#40 /home/vagrant/platform3/backend/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))",
"#41 {main}"
]
}
}

@@ -12,6 +12,7 @@
use League\Fractal\Resource\Collection as FractalCollection;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Contracts\Pagination\Paginator as IlluminatePaginator;
use League\Fractal\TransformerAbstract;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import are ordered by length

@georaldc
Copy link
Contributor

georaldc commented Sep 1, 2016

One solution is to disable eager loading in Dingo and just handle it yourself. Not sure how viable that is though. Probably depends on your codebase

@trycatchjames
Copy link
Contributor

@georaldc that removes half of the benefit of using dingo instead of fractal directly...

@m1r0
Copy link

m1r0 commented Mar 30, 2017

Got the same problem when using model bindings.

@m1r0
Copy link

m1r0 commented Mar 30, 2017

Solved the problem by registering a custom exception:

<?php

namespace App\Providers;

use Dingo\Api\Routing\Helpers;
use Dingo\Api\Exception\Handler as ExceptionHandler;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class ApiServiceProvider extends ServiceProvider
{
    use Helpers;

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        app(ExceptionHandler::class)->register(function (ModelNotFoundException $exception) {
            return $this->response->errorNotFound();
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Not sure if this is really related to the actual pull request but may help someone.

@specialtactics specialtactics self-requested a review February 5, 2019 13:34
@fbingha
Copy link

fbingha commented Feb 15, 2019

Just ran into this issue, the fix provided over 2 years ago resolves the issue for me.

@specialtactics specialtactics merged commit e0c8648 into dingo:master Feb 17, 2019
@specialtactics
Copy link
Member

PR Merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants