Free ebook: common errors and exceptions in laravel

Page 1

Free Ebook

DECODEWEB.IN

Common Laravel Errors and Exceptions


Errors and exceptions are integral parts of software development. In this post I discussed major mistakes by new developers and they get bewildered.

Hello readers ! hope you are doing fine and enjoying my blog. Laravel is beautiful, no doubt, but its error reporting while in development environment is too much haunted. As far as I know everyone has seen a black and white screen with millions of characters arranged line by line, even if there is a semicolon (;) missing. Our today’s topic would be the top 5 common errors / exceptions or simply the mistakes new developers make while working on Laravel which are: 1. 2. 3. 4. 5.

MethodNotAllowedHttpException 419 Error / Page Expired File Permission for storage ReflectionException / Class does not exist] 500 Error

1. MethodNotAllowedHttpException

This is the most frequent occurring exception, as it says “Method is not allowed” but where ? Simply go to web.php or api.php​ where you have written the routes and check if any route / url is using some other type of http method instead of the intended method.


For example, if your route is using POST as method but you are calling as GET. As Laravel is too much concerned about security, it will not allow POST route to be called as GET route hence this exception occurs.

2. 419 Error / Page Expired Again, this exception is based on in-built application security. Error 419 or Page Expired comes when we do not include CSRF_TOKEN in the html form body, since forms are usually the way to POST data to another file or controller.

Excluding routes from CSRF Protection Sometimes there may be situations where we might want to allow some routes without CSRF_TOKEN, we can simply add those URIs or routes in VerifyCsrfToken middleware’s except array like this <?php namespace​ ​App​\​Http​\​Middleware​; use​ ​Illuminate​\​Foundation​\​Http​\​Middleware​\​VerifyCsrfToken​ ​as​ ​Middleware​; class​ V ​ erifyCsrfToken​ ​extends​ ​Middleware { /** * The URIs that should be excluded from CSRF verification. * * @var array */ protected​ $except = [ 'stripe/*'​, 'http://example.com/foo/bar'​, 'http://example.com/foo/*'​, ]; }


3. File Permission for storage

In development environment, laravel generates lots of error logs in storage directory as files named according to dates, for example 2019-08-15.log so to write those In linux distros like Ubuntu, we need to give Super User permission to storage directory. sudo chmod -R 776 ​/storage Same way for bootstrap directory we often get error like this. Hence best practice in would be to give both of these directories access to write. sudo chmod -R 776 ​/bootstrap​ ​/storage

4. ReflectionException / Class does not exist Like this heading suggests, this exception occurs when we give reference to any class but that class is not present at that location. For example, in web.php, we define a route or URI and map it to a particular Controller and its function, if that particular controller class is not present there, that is, path may be wrong or controller itself is not created. Same applies to Models, Events, ServiceProviders etc.


5. 500 Error 500 error is one of the scariest even for experienced developer, no offense, to debug this error first we need to check if server is all good, second check will be how is a logic applied to source code. Many a times, as new developers, we tend to make silly mistakes like, we redirect to the same page, to be specific a controller function, hence on server end its stack gets full thus it is not available to handle more request. If anything goes wrong on server, error code will be the same 500 even if root cause be whatever, try to write sensitive codes in try-catch blocks at least we can be prevented by getting 500 errors.

Original source of information : ​decodeweb.in


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.