8 Common SilverStripe Errors Explained (and solved!)

8 Sep

8 Common SilverStripe Errors Explained (and solved!)

Get Good at Debugging

One of the great paradoxical mixed-blessings of working with SilverStripe is that it is a code-driven development environment. As coders, we love that. The CMS doesn’t get in our way with silly, opinionated turnkey solutions, or bloated interface that is as opaque as the ground we walk on. But for entry-level or even novice PHP developers, SilverStripe can put up a pretty big wall with these requirements, and it’s a difficult product to love and admire when you’re swatting away one cryptic error after another. With the help of this article, you can find solutions to a lot of common SilverStripe errors.

I’m not going to claim that there are a finite number of errors in SilverStripe that each have their own unique solution, but there are patterns and conceptual realms of errors that, when understood, can help you in your debugging, and make you a better developer.

Error Taxonomy

There are two primary types of errors we see in interpreted languages like PHP:

Parse errors are errors that can be detected by the PHP compiler without knowing anything about how your application works. Some examples:

  • You forgot a semicolon
  • You didn’t close your if block with a curly brace
  • Missing parenthesis

These are all parse errors. Think of it as invalid XML. Something just isn’t right, and PHP doesn’t want anything to do with your script.

Runtime errors are a little more complex. The script has been validated, and everything looks cool, but given the circumstances at the time the code is executed, something goes wrong. Examples:

  • You’ve tried to loop through a variable that is not an array
  • You’re accessing a property of a variable that is not an object
  • You called a method on an object that doesn’t exist

Runtime errors tend to be a lot more descriptive in SilverStripe because the Sapphire framework handles these errors, and gives you a nice backtrace with plenty of information (we all know that dreaded denim-blue error screen). That said, they can be complicated to resolve, since they’re conceptual in nature. Parse errors are non-descript, but resolving them is straightforward and matter-of-fact.

The Great Eight

#1. White Screen of Death

Type: Parse (usually)

Explanation: One of the most frustrating errors to deal with, this one has a place in all of our hearts. No error, no output, no clue what to do — the server has simply thrown a 500 response code, and your browser is dead in the water. As it happens, not everyone wants to display their errors. A lot of production sites choose to hide errors because they display sensitive information, such as the full document root.

The Fix: Crank up your PHP error reporting. There are multiple ways to do this. First, you can change the setting from within your PHP script, by adding the following to your _config.php:

error_reporting(E_ALL);

If that doesn’t work, make sure PHP is not just reporting the error, but displaying it, as well. Add this to your .htaccess file:

php_value display_errors On

With those two modifications in place, you will have seen your last white screen of death. I promise.

#2. “There Has Been an Error”

Type: Runtime

Explanation: Equally as frustrating in its haughty disdain for verbosity, this bad boy has been known to cause any of the following: screen punching, mouse hurling, keyboard splitting, and, my favorite, laptop Frisbee. Chill out. There’s an easy fix. You simply haven’t put your SilverStripe site into “dev” mode.

The Fix: Unless this is your production site, you shouldn’t be in “live” mode. Opt for “dev” mode to get verbose error reporting, and many more helpful debugging tools available to you. Simply add this to your _config.php file:

Director::set_environment_type('dev');

Now SilverStripe won’t be so tight-lipped and annoyingly secretive about what the problem is.

#3. “Trying to get property of non-object”

Type: Runtime

Explanation: There could be a lot of reasons for this error, but I’m going to share my favorite cause. You’ve probably gotten a little too assuming with your use of daisy chaining in object-oriented PHP. Daisy-chaining is the technique of running one method against the output of a previously invoked method. For example:

$myPage->RelatedPage()->Link();

We can thank jQuery for the normalization and propagation of this technique. How many of us don’t love writing these types of commands in jQuery?

$('#my-element').parents('div').find('input').filter('disabled').click(some_function);

jQuery has mainstreamed daisy-chaining in object-oriented programming, and unfortunately, it creates bad habits. It works well in jQuery because every method is instructed to return another jQuery object, so there’s little chance of these numerous methods in the chain ever failing.

PHP doesn’t do that, and more over, the Sapphire framework doesn’t do that. In the above example, if the “RelatedPage” method comes up empty, it returns false, which means “Link()” is running against a non-object.

There is at least one exception to this in SilverStripe, however. The SQLQuery object, one of my favorite SilverStripe tools, follows the principles of daisy chaining that we see in jQuery. Every method in the SQLQuery class returns itself, which makes it possible to create some really slick looking code. Just look at this example:

$sql = new SQLQuery();
$result = $sql->select(array("FirstName","LastName"))
			  ->from("JobApplication")
			  ->where("Position","Sales")
			  ->innerJoin("File","`File`.ID = ResumeID")
			  ->orderby("LastName ASC")
			  ->execute();

Slick, isn’t it? This type of syntax can also be seen in the Doctrine ORM.

The Fix: Let’s revise the method above to the following:

if($page = $this->RelatedPage()) {
     return $page->Link();
}
return false;

It doesn’t look as cool, but it looks way cooler than that error.

#4. “Filename cannot be empty”

Type: Runtime

Explanation: This is an easy one. SilverStripe doesn’t check to see if a file exists before including it in the template (last I checked). You’ve attempted to include a template that doesn’t exist.

The Fix: Review your templates for the current page and ensure that all the <% include %>  tags point to valid .ss files in your /Includes/ folder.

#5. “Unknown Column ParentID in WHERE Clause”

Type: Runtime (database)

Explanation: A classic DataObjectManager and ComplexTableField error, this one is caused by a mal-formed datamodel. SilverStripe is trying to retrieve the records for your DOM or CTF that are related to the current page, but it can’t figure out the foreign key, so it assumes “ParentID”.

The Fix: Always remember to reciprocate a $has_many relationship with a $has_one on the child object. For instance, if you have:

class StaffPage extends Page {
     static $has_many = array (
          'StaffMembers' => 'StaffMember'
     );
}

Remember to add the $has_one in your DataObject:

class StaffMember extends DataObject {
     static $has_one = array (
           'StaffMemberPage' => 'StaffMemberPage'
     );
}

#6. “Javascript Parse Error” (CMS error message)

Type: Runtime/Parse

Explanation: The CMS has a very robust way of handling AJAX responses. It has to load in all the script included in the head, and update the CMS interface to reflect your change, so if that response doesn’t come back as expected, it’s not PHP that is going to complain — it’s the Javascript parser. That said, however, the CMS is equipped to read HTTP error codes, so usually this error happens because you’ve added some output where it doesn’t belong.

The Fix: Activate Firebug and check the response text coming back. It’s a little painstaking, but you’ll figure it out.

#7. “Cookie “bypassStaticCache” could not be set”

Type: Runtime

Explanation: “bypassStaticCache” is the first cookie to be set in the SilverStripe bootstrap. If you have rendered output to the browser before that happens, the cookie cannot be set. The same is true for session variables.

The Fix: Check your _config.php files for any output. Done any debugging with some var_dump() or echo functions? If not, you’re probably sending whitespace to the browser unknowingly. This can easily happen in SilverStripe or any other MVC framework if you have closed your PHP tags in any .php file that is not a template. That is, if below the closing ?> tag, you have entered a line break, this will cause problems, since whitespace qualifies as content sent to the browser.

Remove all closing PHP tags. In a typical SilverStripe project, there is nowhere they belong, since SilverStripe uses its own templating language. If you’ve found a reason to close PHP tags in any SilverStripe project, I invite you to prove me wrong in a comment below.

Lastly, check all your PHP files and make sure the first line of the file is an opening <?php tag.

#8. Parse error on .ss file

Type: Parse

Explanation: SilverStripe templates, despite their glorified (Rails worshipping) template syntax, ultimately end up being evaluated as PHP. There really isn’t much magic there. So if you have a basic PHP syntax error, it won’t fail gracefully. You’ll just get a fatal parse error, as you do in any other PHP file.

The Fix: If you’re in dev mode, add ?showtemplate=1 to the URL. That will show you the template after it has been converted to PHP. The error has probably given you a line number, so find your way down to that line, and fix the problem in your template. (Hint: It’s almost always one too many or one too few <% end_if %> or <% end_control %> statements).

A Few Thoughts on Error Reporting

Most of what has been discussed in this article applies to sites that are in development. Once in production, handling errors is much less about interpreting and resolving them than it is recording them. SilverStripe provides some valuable tools that you can use to log and report errors as they happen on your production site.

Sending an email to the administrator

SS_Log::add_writer(new SS_LogEmailWriter('your@email.com'), SS_Log::ERR);

Writing the error to a flat file

SS_Log::add_writer(new SS_LogFileWriter('ss_errors.txt'), SS_Log::ERR);

Final Thoughts

Errors are a necessary evil in an open framework like SilverStripe. If you have any nasty errors you encounter frequently, please share them below!

34 Responses to “8 Common SilverStripe Errors Explained (and solved!)”

  1. Matthew Balaam 09. Sep, 2010 at 6:14 am #

    Nice article. I only starting touching PHP when someone showed me Silverstripe about 12 months ago and almost every time these messages have been a complete mystery to me, but your descriptions should help me a lot in future.

    • Dotty 13. May, 2011 at 5:02 am #

      That’s more than ssnieble! That’s a great post!

  2. Simon Welsh 09. Sep, 2010 at 7:00 am #

    Could you provide a method for error 1 which doesn’t break most shared hosts? php_value will only work if PHP’s running as mod_php and if the host allows it.

    Something like ini_set(‘display_errors’, ‘On’); is far more likely to work.

    Also, for #6, could you not assume we’re all using Firefox? There is a growing push towards WebKit based browsers for development, which have their own, inbuilt, inspector.

  3. Fred Condo 09. Sep, 2010 at 11:28 am #

    Nice article! A couple things:

    #2: if using _ss_environment.php (which is much more convenient than using _config.php alone), set dev mode with:
    define( ‘SS_ENVIRONMENT_TYPE’, ‘live’ );
    See http://doc.silverstripe.org/environment-management

    email: email.com is a real domain. In documentation, best to use example.com/net/org, which are reserved for examples

    • Fred Condo 09. Sep, 2010 at 11:30 am #

      and of course I meant
      define( ‘SS_ENVIRONMENT_TYPE’, ‘dev’ );

    • unclecheese 09. Sep, 2010 at 11:43 am #

      Hi, Fred,

      Thanks so much for your feedback.

      Looking back, I think I intentionally left out _ss_environment.php because, in my experience, so many people don’t know anything about it or even use it, as invaluable as it is. Chances are, if you don’t know about dev mode yet, _ss_environment is a little over your head. In light of that, I didn’t want to open up a whole new discussion about _ss_environment.php and start answering all kinds of questions. Rather, I think it deserves its own post about all of its awesome benefits… if that makes any sense. :)

  4. Ingo Schommer 09. Sep, 2010 at 4:29 pm #

    Brilliantly written, thanks Aaron! Exactly what we need for the documentation relauch if your keen to contribute (http://groups.google.com/group/silverstripe-documentation)

    Lets hope that together we can make these errors less frequent – for example the “bypassStaticCache” error is avoidable, or at least can have a more friendly error message.

    Do you guys think its a good idea to point developers to setting “dev mode” in a “live mode” error message (“There has been an error”)? It has the potential to confuse visitors to a live site (which is the intended case for this message), but its a very frequent cause of frustration in SilverStripe.

    • unclecheese 09. Sep, 2010 at 5:19 pm #

      Thanks, Ingo!

      To answer your question, I’d estimate that of my 3,500 forum posts, at least 300 have been instructions on putting a site in dev mode. I’m not sure what the right course of action is for pointing people to a solution, however. The idea of an end user reading something about “dev” mode seems a little inappropriate. What about if the link only displayed if the user was an admin?

      • Marijn Kampf 16. Sep, 2010 at 5:47 am #

        In the error why don’t you add a link saying “View a more detailed error report”. That either puts the site into dev mode, ore explains to the user how it can put the site in dev mode. I don’t think such a link would confuse an ordinary user and will provide the next step for someone looking to solve the error.

        • Paul Bennett 21. Dec, 2010 at 5:54 pm #

          Hi Marijn,

          Doing this may expose technical details of the site (possibly including db structure and table names) to non-admin users, which is not a good thing security-wise.

          An admin / logged in check would get around this (show ‘more details’ link if user logged in / sufficient privilege levels)…?

  5. Sean Harvey 09. Sep, 2010 at 5:41 pm #

    Hi Aaron,

    Nice write up here.

    Putting the site into development mode can also be achieved by adding ?isDev=1 to the end of the URL.

    This is slightly easier than the alternative of setting Director::set_environment_type(‘dev’) in the mysite/_config.php file.

    Again very useful, this should make people starting out with SilverStripe very happy!

    Sean

  6. abyss 12. Sep, 2010 at 5:02 am #

    Hi Aaron,

    I am quite a newbie to SilverStripe. Found the link to your site while working through the official SS tutorials.

    I really appreciate your module contributions and the tutorials and podcast you created for this amazing system. Keep up the great work.

    Regards

  7. Martijn 13. Sep, 2010 at 2:44 pm #

    In addition to point 3 I would like to add the isset check. I always get punished sooner or later when i’m too lazy to add something like

    if(isset($this->request)){//}

    before actually calling a property of $this->request.

  8. Frank 05. Oct, 2010 at 11:41 pm #

    Thanks for the useful post – bookmarked. Thought I would add 2 cents about writing errors to a flat file – the message has to be an Exception or array, not just a string which tripped me up a few times.

    //Logging
    SS_Log::add_writer(new SS_LogFileWriter(‘/var/www/silverstripe/mysite/errors.log’));
    SS_Log::log(new Exception(‘Some log message here’), SS_Log::NOTICE);

    $message = array(
    ‘errno’ => ”,
    ‘errstr’ => ‘Some log message here’,
    ‘errfile’ => ”,
    ‘errline’ => ”,
    ‘errcontext’ => ”
    );
    SS_Log::log($message, SS_Log::NOTICE);

  9. alex 18. Nov, 2010 at 5:36 pm #

    very helpful tutorial for debugging..
    Thanks lots U.C.

  10. dalesaurus 22. Nov, 2010 at 4:22 pm #

    Another Lovely one: When customizing the FieldSet used in the CMS with getCMSFields() or when decorating with updateCMSFields()

    [User Error] FieldSet::addFieldToTab() Tried to add a tab to object ‘FieldSet’ – ‘FieldDataElements’ didn’t exist.

    You didn’t specify a dot notation hierarchy with ‘Root.xxxxxxx’ when calling $fields->findOrMakeTab() or $fields->addFieldToTab().

  11. Paul Bennett 21. Dec, 2010 at 5:59 pm #

    Legendary post. Wish this existed – and I’d found it 2 years ago.

    Much pain would have been avoided :)

  12. Ingo 17. Jan, 2011 at 7:31 pm #

    This is now linked from the official docs as well: http://doc.silverstripe.org/sapphire/en/installation/common-problems – thanks very much for producing such an awesome guide!

  13. MrFixer 18. Feb, 2011 at 5:03 pm #

    Thanks for this easy to read explanation of the more common SS errors, being new to SS i am hitting that pretty big wall that you mention, this troubleshooter post of yours has helped explain things in a manner i can understand.. regards

  14. Sumesh M.S 11. Apr, 2011 at 7:56 am #

    Respected sir,

    This is regarding Search in silverstripe all Versions.

    The normal function of search : If we enter some query and click search, then the search would obviously search for the answer to the query asked by the user and displays it.

    Case 2 : If we enter nothing in the search field and click on the search button, Now the search is listing the entire content from the website.

    My Question is : How can we display an error messag stating some message. May be the ” Your search did not match anything in this website ”

    The point is that, I need an error message, when the user doesn’t input a query and clicks go.

  15. lokoprof 12. Apr, 2011 at 7:16 pm #

    how can i rectify error below in xampp

    import_request_variables() [function.import-request-variables]: No prefix specified – possible security hazard in C:\xampp\htdocs\engine\auth.php on line 9

    tanks

  16. IJas 21. Nov, 2011 at 2:05 am #

    I am trying to install PayPal express checkout plugin. When I try to build the system using dev/build, I get the Blank page. I have followed the instruction on this blank page issue. But I’m still getting the error. Please give me a solution

  17. Jake 05. Feb, 2012 at 7:59 pm #

    This page is officially bookmarked.

    I look forward to trying out SilverSmith.

    Keep up the good work!

  18. rokryan 02. Mar, 2012 at 2:34 am #

    Does anyone, by any chance happen to know what is going on with these errors: http://westvine.com.au/

    I’m trying to transfer this website: http://westvine.designincentive.com/ (SS v 2.4.5) to a different server (Westnet for those playing at home) but have stumbled on errors I’ve never seen before.. Is this the white screen of death?

    Files & database are in place but I’m thinking this issue has something to do with the htaccess file or a particular PHP config..

    Any assistance would be most appreciated!

  19. Anish 24. May, 2012 at 8:33 am #

    Hi, I am getting the following error during SS installation.
    [Notice] Array to string conversion
    POST /silver/install.php
    Line 396 in /usr/home/itcell/web/silver/sapphire/core/model/Database.php

    My server is FreeBSD,Nginx, Mysql

  20. Chester Lohoff 30. Jun, 2012 at 7:34 am #

    My partner and I absolutely love your blog and find
    many of your post’s to be what precisely I’m looking for. Do you offer guest writers to write content for yourself? I wouldn’t mind creating a post or elaborating on a number of the subjects you write concerning here. Again, awesome weblog!

    • unclecheese 03. Jul, 2012 at 11:03 am #

      Sure, send me any ideas you have and we can talk. Thanks for your interest!

  21. iphone updates 02. Jul, 2012 at 12:36 pm #

    Hi, i think that i saw you visited my weblog so i came
    to “return the favor”.I’m trying to find things to enhance my website!I suppose its ok to use some of your ideas!!

  22. pilates Cape town 02. Oct, 2012 at 4:52 pm #

    Thoroughly great blog post 8 Common SilverStripe Errors Explained (and solved!
    ) | Left and Main.. Carry on blogging.

  23. bishops court 02. Oct, 2012 at 5:19 pm #

    Hello There!
    I totally respect your blog! Great job on the page design.

    Cheers…

  24. Archie 14. Nov, 2012 at 8:38 pm #

    bοokmаrκeԁ!!, I really like your blοg!

  25. Yunus 11. Feb, 2013 at 8:33 am #

    Even if i am on some other tab under pages, when i save and
    publish every time i loose my current tab and gets shifted to main
    content tab.. So i have to go back to the tab where i was working.
    It becomes quite hectic. Any solutions..????

  26. Hellen 27. Feb, 2013 at 11:57 am #

    Hey! I know this is kind of off topic but I was wondering if you knew where I could get a captcha plugin
    for my comment form? I’m using the same blog platform as yours and I’m having problems finding one?
    Thanks a lot!

  27. Talha Khan 03. Jun, 2013 at 2:34 pm #

    When I am accessing my site at “http://www.characterfirst.com/sapphire/main.php”, I’m facing the following error, and its been two days i’m unable to fix it. any suggestions?

    [Warning] require_once(HTTP/Request.php) [function.require-once]: failed to open stream: No such file or directory
    GET /sapphire/main.php

    Line 3 in /nfs/c04/h01/mnt/23363/domains/characterfirst.com/html/wrapper_page/WrapperPage.php

Leave a Reply