Falhando miseralvelmente com PHP

Preview:

DESCRIPTION

Erros têm de fazer parte do dia a dia do desenvolvedor, porém a maioria prefere ignorá-los ou simplesmente não sabem como lidar com os mesmos. Nesta palestra de 50min vamos passar por duas diferentes formas de lidar com eles: procedural/imperativo e Exceptions além de ver como manter e utilizar mensagens de erros a fim de serem úteis no seu dia a dia.

Citation preview

Falhando miseravelmente com PHPAugusto Pascutti

50min para resolver:

A página ta toda em branco aqui, de novo.

Usuário do sistema

Como eu faço pro PHP não exibir esse monte de E_NOTICE chato?

Desenvolvedor querendo fazer merda

Tem um E_WARNING e alguma coisa sobre data na página.

Usuário do sistema

Eu entro e dá erro.Usuários

50 minutos sobre:

Erros e outras mensagens relevantes

Utilidade dessas mensagens

Geração dessas mensagens

Errose outras mensagens relevantes

Since the beginning, life has relied upon the transmission of

messages.RFC 3164:The BSD syslog Protocol

SeveridadesTipos de mensagens relevantes

Severidades:• Emergency: deu merda. Sistema inutilizado

• Alert: vai da merda. Faça alguma coisa logo

• Critical: tem tudo pra dar merda

• Error: coisas que não deveriam acontecer

• Warning: aviso. Tome mais cuidado

• Notice: normal, mas ainda assim significativo

• Informational

• Debug

Severidades precisam de um contexto

Eu mesmo, agora há pouco.

Severidades no PHPOu as utilizadas e disponíveis no PHP de todo mundo.

E_ERRORUm erro sem recuperação, a execução é interrompida.

E_PARSEErros de sintaxe, a execução é interrompida.

E_WARNINGUm erro não fatal, a execução continua.

E_NOTICEIndica um erro muito provável, a execução continua.

E_DEPRECATEDIndica uso de código que deixará de funcionar em

versões futuras.

E_STRICTSugestões para melhor interoperabilidade e

compatibilidade futura.

Utilidadedessas mensagens

Um arquivo de log é como uma mulher: quanto mais gostosa(o),

mais atenção você presta.Newton

1.202.218.8 - - [20/Jun/2012:19:05:12 +0200] "GET /robots.txt HTTP/1.0" 404 492 "-" "\"Mozilla/5.0"208.115.113.91 - - [20/Jun/2012:19:20:16 +0200] "GET /logs/?C=M;O=D HTTP/1.1" 200 1278 "-" "Mozilla/5.0 (compatible; Ezooms/1.0; ezooms.bot@gmail.com)"123.125.71.20 - - [20/Jun/2012:19:30:40 +0200] "GET / HTTP/1.1" 200 912 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"220.181.108.101 - - [20/Jun/2012:19:31:01 +0200] "GET / HTTP/1.1" 200 912 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"123.125.68.79 - - [20/Jun/2012:19:53:24 +0200] "GET / HTTP/1.1" 200 625 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"178.154.210.252 - - [20/Jun/2012:19:54:10 +0200] "GET /?C=S;O=A HTTP/1.1" 200 663 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)"74.125.126.102 - - [20/Jun/2012:20:15:28 +0200] "GET / HTTP/1.1" 200 606 "http://www.google.com/url?sa=t&rct=j&q=error&source=web&cd=1&ved=0CFAQFjAG&url=http%3A%2F%2Fwww.isrolab.com%2F&ei=GxPiT5PsL4e04AHPtgE&usg=AFQjCNHnmjmdkUV584ORIpOXz7zAPX0UHQ" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"74.125.126.103 - - [20/Jun/2012:20:15:29 +0200] "GET /icons/blank.gif HTTP/1.1" 200 383 "http://www.isrolab.com/" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"74.125.126.93 - - [20/Jun/2012:20:15:29 +0200] "GET /icons/folder.gif HTTP/1.1" 200 460 "http://www.isrolab.com/" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"74.125.126.82 - - [20/Jun/2012:20:15:30 +0200] "GET /favicon.ico HTTP/1.1" 404 449 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"184.82.92.239 - - [20/Jun/2012:21:03:44 +0200] "GET /logs/access.log HTTP/1.1" 200 2519 "http://isrolab.com/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 3.5.30729)"173.236.21.106 - - [20/Jun/2012:21:16:22 +0200] "GET /robots.txt HTTP/1.0" 404 488 "-" "Mozilla/5.0 (compatible; MJ12bot/v1.4.3; http://www.majestic12.co.uk/bot.php?+)"173.236.21.106 - - [20/Jun/2012:21:16:23 +0200] "GET / HTTP/1.0" 200 621 "-" "Mozilla/5.0 (compatible; MJ12bot/v1.4.3; http://www.majestic12.co.uk/bot.php?+)"213.186.122.2 - - [20/Jun/2012:21:27:53 +0200] "GET /logs/?C=D;O=D HTTP/1.1" 200 658 "-" "Mozilla/5.0 (compatible; AhrefsBot/3.0; +http://ahrefs.com/robot/)"66.249.72.65 - - [20/Jun/2012:21:28:00 +0200] "GET /robots.txt HTTP/1.1" 404 508 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"66.249.72.65 - - [20/Jun/2012:21:28:00 +0200] "GET /logs/ HTTP/1.1" 200 723 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"123.125.71.44 - - [20/Jun/2012:21:38:57 +0200] "GET / HTTP/1.1" 200 913 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"220.181.108.88 - - [20/Jun/2012:21:39:48 +0200] "GET / HTTP/1.1" 200 913 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"178.154.210.252 - - [20/Jun/2012:21:45:12 +0200] "GET /logs/ HTTP/1.1" 200 728 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)"139.18.2.209 - - [20/Jun/2012:22:31:43 +0200] "GET / HTTP/1.1" 200 912 "-" "findlinks/2.6 (+http://wortschatz.uni-leipzig.de/findlinks/)"123.125.71.48 - - [20/Jun/2012:22:38:14 +0200] "GET / HTTP/1.1" 200 913 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"220.181.108.95 - - [20/Jun/2012:22:39:03 +0200] "GET / HTTP/1.1" 200 913 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0;

Pegue já seu exemplo!!g filetype:log access.log

Eu não preciso disso, já tenho o Analytics em todas as páginas.

Algum desenvolvedor

Como usar isso?O (meu) jeito *nix

word, line, character, and byte count

wc

#!/bin/bash[~/Apache] $ wc -l access.log 65063 access.log

print lines matching a pattern

grep

#!/bin/bash[~/Apache] $ wc -l access.log 65063 access.log

[~/Apache] $ grep "HTTP/1.1" access.log | wc -l 52129

#!/bin/bash[~/Apache] $ wc -l access.log 65063 access.log

[~/Apache] $ grep "HTTP/1.1" access.log | wc -l 52129

[~/Apache] $ grep "HTTP/1.0" access.log | wc -l 13007

#!/bin/bash[~/Apache] $ wc -l access.log 65063 access.log

[~/Apache] $ grep "HTTP/1.1" access.log | wc -l 52129

[~/Apache] $ grep "HTTP/1.0" access.log | wc -l 13007

[~/Apache] $ grep -v "HTTP/1.1" access.log | grep -v "HTTP/1.0"114.66.65.89 - - [24/Dec/2012:07:46:27 +0100] "GET /logs/access.log v\xc2\xa4N\xd41.0" 200 1963700 "http://www.isrolab.com/" "Mozilla/5.0 (Windows NT 6.1; rv:2.0b7pre) Gecko/20100921 Firefox/4.0b7pre"

#!/bin/bash[~/Apache] $ wc -l access.log 65063 access.log

[~/Apache] $ grep "HTTP/1.1" access.log | wc -l 52129

[~/Apache] $ grep "HTTP/1.0" access.log | wc -l 13007

[~/Apache] $ grep -v "HTTP/1.1" access.log | grep -v "HTTP/1.0"114.66.65.89 - - [24/Dec/2012:07:46:27 +0100] "GET /logs/access.log v\xc2\xa4N\xd41.0" 200 1963700 "http://www.isrolab.com/" "Mozilla/5.0 (Windows NT 6.1; rv:2.0b7pre) Gecko/20100921 Firefox/4.0b7pre"

Procura isso no Analytics agora.Augusto Pascutti

display first lines of a file

head

#!/bin/bash[~/Apache]$ head -n 1 access.log1.202.218.8 - - [20/Jun/2012:19:05:12 +0200] "GET /robots.txt HTTP/1.0" 404 492 "-" "\"Mozilla/5.0"

pattern-directed scanning and processing language

awk

#!/bin/bash[~/Apache]$ head -n 1 access.log1.202.218.8 - - [20/Jun/2012:19:05:12 +0200] "GET /robots.txt HTTP/1.0" 404 492 "-" "\"Mozilla/5.0"

[~/Apache]$ head -n 1 access.log | awk '{print $1}'1.202.218.8

#!/bin/bash[~/Apache]$ head -n 1 access.log1.202.218.8 - - [20/Jun/2012:19:05:12 +0200] "GET /robots.txt HTTP/1.0" 404 492 "-" "\"Mozilla/5.0"

[~/Apache]$ head -n 1 access.log | awk '{print $1}'1.202.218.8

[~/Apache]$ awk '{print $1}' access.log | head -n 101.202.218.8208.115.113.91123.125.71.20220.181.108.101123.125.68.79178.154.210.25274.125.126.10274.125.126.10374.125.126.9374.125.126.82

sort lines of text files

sort

#!/bin/bash[~/Apache]$ awk '{print $1}' access.log | head -n 51.202.218.8208.115.113.91123.125.71.20220.181.108.101123.125.68.79

#!/bin/bash[~/Apache]$ awk '{print $1}' access.log | head -n 51.202.218.8208.115.113.91123.125.71.20220.181.108.101123.125.68.79

[~/Apache]$ awk '{print $1}' access.log | head -n 5 | sort1.202.218.8123.125.68.79123.125.71.20208.115.113.91220.181.108.101

report or filter out repeated lines in a file

uniq

#!/bin/bash[~/Apache]$ awk '{print $1}' access.log | sort | uniq | wc -l 7967

#!/bin/bash[~/Apache]$ awk '{print $1}' *.log | sort | uniq | wc -l 7967

[~/Apache]$ awk '{print $1}' *.log | sort | uniq | head -n 101.0.250.1211.179.147.21.2.209.1301.2.215.1511.2.216.1101.2.217.901.202.218.131.202.218.141.202.218.151.202.218.21

Quais os IPs que mais acessaram o access.log dele?

Já que sabemos que não fomos os únicos.

Você já ouviu falar em “bash script”, certo?

Eu

#!/bin/bash[~/Apache]$ for ip in `awk '{print $1}' *.log | sort | uniq`; do > count=`grep “$ip” access.log | grep 'access.log' | wc -l`; > echo "$count $ip" >> ip-safadenhos.txt; > done;

#!/bin/bash[~/Apache]$ wc -l *.txt 9479 ip-safadenhos.txt

#!/bin/bash[~/Apache]$ wc -l *.txt 9479 ip-safadenhos.txt

[~/Apache]$ awk '{print $1}' *.txt | grep -v 0 | wc -l 5619

Top 10

#!/bin/bash[~/Apache]$ sort -nr ip-safadenhos.txt | head -n 10 246 88.250.57.232 184 216.151.130.179 121 88.190.16.36 90 199.59.56.24 84 80.93.217.38 73 216.152.251.7 71 89.233.105.125 68 120.196.161.251 68 120.196.161.251 58 190.254.21.236

Como usar isso no dia a dia?Já que ninguém tem tempo sobrando.

Pegue já seu outro exemplo!!g filetype:log error.log E_ERROR

Log do PHPVia linha de comando.

[/home/bravecreate/www/hiroshima-gift.co.jp/data/class/pages/admin/LC_Page_Admin.php(69)] from 126.29.228.69login_id = ()[acf83d9069c4c9036fa852909503b631]2013/01/30 00:48:05 [/hiroshima-gift.co.jp/html/admin/index.php] Fatal error(E_ERROR): Call to a member function doAction() on a non-object on [/home/bravecreate/www/hiroshima-gift.co.jp/data/class/pages/admin/LC_Page_Admin.php(69)] from 126.29.228.69login_id = ()[acf83d9069c4c9036fa852909503b631]2013/01/30 00:48:07 [/hiroshima-gift.co.jp/html/admin/index.php] Fatal error(E_ERROR): Call to a member function doAction() on a non-object on [/home/bravecreate/www/hiroshima-gift.co.jp/data/class/pages/admin/LC_Page_Admin.php(69)] from 126.29.228.69login_id = ()[acf83d9069c4c9036fa852909503b631]2013/01/30 00:49:01 [/hiroshima-gift.co.jp/html/admin/index.php] Fatal error(E_ERROR): Call to a member function doAction() on a non-object on [/home/bravecreate/www/hiroshima-gift.co.jp/data/class/pages/admin/LC_Page_Admin.php(69)] from 126.29.228.69login_id = ()[acf83d9069c4c9036fa852909503b631]2013/01/30 00:49:03 [/hiroshima-gift.co.jp/html/admin/index.php] Fatal error(E_ERROR): Call to a member function doAction() on a non-object on [/home/bravecreate/www/hiroshima-gift.co.jp/data/class/pages/admin/LC_Page_Admin.php(69)] from 126.29.228.69login_id = ()[acf83d9069c4c9036fa852909503b631]2013/01/30 00:50:38 [/hiroshima-gift.co.jp/html/admin/index.php] Fatal error(E_ERROR): Call to a member function doAction() on a non-object on [/home/bravecreate/www/hiroshima-gift.co.jp/data/class/pages/admin/LC_Page_Admin.php(69)] from 126.29.228.69login_id = ()[acf83d9069c4c9036fa852909503b631]2013/01/30 00:50:59 [/hiroshima-gift.co.jp/html/admin/index.php] Fatal error(E_ERROR): Call to a member function doAction() on a non-object on [/home/bravecreate/www/hiroshima-gift.co.jp/data/class/pages/admin/LC_Page_Admin.php(69)] from 126.29.228.69login_id = ()[acf83d9069c4c9036fa852909503b631]2013/04/02 14:09:39 [/hiroshima-gift.co.jp/html/index.php] Fatal error(E_USER_ERROR): DB処çê †ãÅ §ã‚¨ãƒ©ãƒ¼ãÅ Œç™ºç”ŸãÅ —ãÅ ¾ãÅ —ãÅ Ÿã€‚SQL: [SET SESSION storage_engine = InnoDB]PlaceHolder: [array ()]MDB2 Error: insufficient permissions_doQuery: [Error message: Could not select the database: bravecreate_nagasakiya_ec][Last executed query: SET SESSION storage_engine = InnoDB][Native code: 1044][Native message: Access denied for user 'bravecreate'@'%' to database 'bravecreate_nagasakiya_ec'] on [/home/bravecreate/www/hiroshima-gift.co.jp/data/class/SC_Query.php(1008)] from 66.249.74.233customer_id = /home/bravecreate/www/hiroshima-gift.co.jp/html/index.php(32): LC_Page_Index_Ex->init/home/bravecreate/www/hiroshima-gift.co.jp/data/class_extends/page_extends/LC_Page_Index_Ex.php(47): LC_Page_Index->init/home/bravecreate/www/hiroshima-gift.co.jp/data/class/pages/LC_Page_Index.php(45): LC_Page->init/home/bravecreate/www/hiroshima-gift.co.jp/data/class/pages/LC_Page.php(97): SC_Helper_PageLayout->sfGetPageLayout/home/bravecreate/www/hiroshima-gift.co.jp/data/class/helper/SC_Helper_PageLayout.php(54): SC_Helper_PageLayout->getPageProperties/home/bravecreate/www/hiroshima-gift.co.jp/data/class/helper/SC_Helper_PageLayout.php(112): SC_Query::getSingletonInstance/home/bravecreate/www/hiroshima-gift.co.jp/data/class/SC_Query.php(104): SC_Query->__construct/home/bravecreate/www/hiroshima-gift.co.jp/data/class/SC_Query.php(88): SC_DB_DBFactory_MYSQL->initObjQuery/home/bravecreate/www/hiroshima-gift.co.jp/data/class/db/dbfactory/SC_DB_DBFactory_MYSQL.php(323): SC_Query->exec/home/bravecreate/www/hiroshima-gift.co.jp/data/class/SC_Query.php(231): SC_Query->query/home/bravecreate/www/hiroshima-gift.co.jp/data/class/SC_Query.php(814): SC_Query->execute/home/bravecreate/www/hiroshima-gift.co.jp/data/class/SC_Query.php(971): SC_Query->error/home/bravecreate/www/hiroshima-gift.co.jp/data/class/SC_Query.php(1008): trigger_error2013/04/02 14:09:39 [/hiroshima-gift.co.jp/html/index.php] Fatal error(E_ERROR): Call to a member function doAction() on a non-object on [/home/bravecreate/www/hiroshima-gift.co.jp/data/class/SC_View.php(137)] from 66.249.74.233customer_id = 2013/04/02 22:31:14 [/hiroshima-gift.co.jp/html/index.php] Fatal error(E_USER_ERROR): DB処çê †ãÅ §ã‚¨ãƒ©ãƒ¼ãÅ Œç™ºç”ŸãÅ —ãÅ ¾ãÅ —ãÅ Ÿã€‚SQL: [SET SESSION storage_engine = InnoDB]PlaceHolder: [array ()]

#!/bin/bash[~/PHP]$ awk '/(E_[A-Z])/ {print $5}' error.log | sort | uniq -c 24 error(E_ERROR): 15 error(E_USER_ERROR):

#!/bin/bash[~/PHP]$ awk '/(E_[A-Z])/ {print $5}' error.log | sort | uniq -c 24 error(E_ERROR): 15 error(E_USER_ERROR):

[~/PHP]$ awk -F: '/E_ERROR/ {print $4}' error.log | sort | uniq Call to a member function doAction() on a non-object on [/home/bravecreate/www/hiroshima-gift.co.jp/data/class/SC_View.php(137)]

Call to a member function doAction() on a non-object on [/home/bravecreate/www/hiroshima-gift.co.jp/data/class/SC_View.php(137)]

Call to a member function doAction() on a non-object on [/home/bravecreate/www/hiroshima-gift.co.jp/data/class/pages/admin/LC_Page_Admin.php(69)]

Geraçãodessas mensagens

ConfiguraçõesOu coisas que podem provocar diferentes resultados

no seu PHP.

display_errorsVocê quer ver esses erros?

ini_set(‘display_errors’, ‘On’)Exibe erros em HTML para usuário.

(Ideal para ambiente de desenvolvimento)

ini_set(‘display_errors’, ‘Off’)Não exibe erros.

(Ideal para ambiente de produção)

log_errorsEnvia os erros para o arquivo de log configurado.

ini_set(‘log_errors’, ‘On’)Salva os erros no arquivo de log.(Ideal em todos os ambientes)

ini_set(‘log_errors’, ‘Off’)Não salva erros no arquivo de log.

(Não sei porque você irá querer isso)

error_logOnde você deseja que o arquivo de log seja gerado?

error_reportingQuais severidades utilizar?

1 <?php 2 ini_set('display_errors', 1); 3 error_reporting(E_ALL); 4 5 if ($_POST['user']) { 6 // ... 7 } 8 /** 9 * Output: 10 * 11 * PHP Notice: Undefined index: user in notices.php on line 5 12 */

1 <?php 2 ini_set('display_errors', 1); 3 error_reporting(E_ALL ^ E_NOTICE); 4 5 if ($_POST['user']) { 6 // ... 7 } 8 /** 9 * Output: 10 * 11 */

1 <?php 2 ini_set('display_errors', 1); 3 error_reporting(E_ALL); 4 5 echo date('l').PHP_EOL; 6 7 date_default_timezone_set('America/Sao_Paulo'); 8 9 echo date('l').PHP_EOL; 10 11 /** 12 * Output: 13 * 14 * PHP Warning: date(): It is not safe to rely on the system's 15 * timezone settings. You are *required* to use the date.timezone 16 * setting or the date_default_timezone_set() function. 17 * In case you used any of those methods and you are still getting 18 * this warning, you most likely misspelled the timezone identifier. 19 * We selected the timezone 'UTC' for now, but please set date.timezone 20 * to select your timezone. in date.php on line 5 21 * 22 * Saturday 23 * Saturday 24 */

1 <?php 2 ini_set('display_errors', 1); 3 error_reporting(E_ALL); 4 5 include 'nao-existe.php'; 6 7 /** 8 * Output: 9 * 10 * PHP Warning: include(nao-existe.php): failed to open stream: 11 * No such file or directory in warning.php on line 5 12 */

1 <?php 2 ini_set('display_errors', 1); 3 error_reporting(E_ALL); 4 5 require 'nao-existe.php'; 6 /** 7 * Output: 8 * 9 * PHP Warning: require(nao-existe.php): failed to open stream: 10 * No such file or directory in warning.php on line 5 11 * 12 * Fatal error: require(): Failed opening required 13 * 'nao-existe.php' in error.php on line 5 14 */

Procedural

trigger_error()Gera uma mensagem de determinada severidade.

1 <?php 2 date_default_timezone_set('America/Sao_Paulo'); 3 error_reporting(-1); 4 ini_set('log_errors', 1); 5 ini_set('error_log', 'duh.log'); 6 7 define('APP_ENV', getenv('APP_ENV') ?: 'dev'); 8 switch (APP_ENV) { 9 case 'live': 10 ini_set('display_errors', 0); 11 break; 12 default: 13 ini_set('display_errors', 1); 14 break; 15 } 16 17 if (false === extension_loaded('xdebug')) { 18 trigger_error('Conhece o xDebug?', X_USER_NOTICE); 19 } 20 21 if (false === version_compare($current=PHP_VERSION, $required='6.4.0', '>=')) { 22 $msg = sprintf('Versão mínima do PHP requerida %s. (%s)', $required, $current); 23 trigger_error($msg, E_USER_ERROR); 24 unset($required, $current); 25 } 26 27 if (false === file_exists($autoload = 'vendor/autoload.php')) { 28 trigger_error('Por favor, rode a instale com `make install`.', E_USER_ERROR); 29 } 30

error_log()Envia mensagem para o log de erros.

1 <?php 2 ini_set('display_errors', 0); 3 ini_set('log_errors', 1); 4 error_reporting(-1); 5 6 error_log('É nóis na fita!'); 7 error_log('Não me deixa cair no SPAM!', 1, 'a@a.com');

1 <?php 2 ini_set('display_errors', 0); 3 ini_set('log_errors', 1); 4 error_reporting(-1); 5 6 error_log('É nóis na fita!'); 7 error_log('Não me deixa cair no SPAM!', 1, 'a@a.com');

ExceptionsErros num paradigma Orientado a Objetos

1 <?php 2 3 namespace Math; 4 5 class Operation 6 { 7 public static function sum() 8 { 9 $args = func_get_args(); 10 $result = 0; 11 foreach ($args as $arg) { 12 if (false == is_int($arg)) { 13 $msg = 'Somente inteiros permitidos'; 14 throw new \InvalidArgumentException($msg); 15 } 16 $result += (integer) $arg; 17 } 18 19 return $result; 20 } 21 } 22 23 echo Operation::sum(1,2,3,5.0);

1 <?php 2 3 namespace Math; 4 5 require 'math.php'; // use autoload, por favor! 6 7 use InvalidArgumentException; 8 use Exception; 9 10 class Grades 11 { 12 public static function average($prova1, $prova2, $prova3) 13 { 14 try { 15 $total = Operation::sum($prova1, $prova2, $prova3); 16 } catch (InvalidArgumentException $e) { 17 $msg = 'Alguma nota esta errada.'; 18 throw new InvalidArgumentException($msg, null, $e); 19 } catch (Exception $e) { 20 $msg = 'Calcular media de notas.'; 21 throw new RuntimeException($msg, null, $e); 22 } 23 } 24 } 25 26 echo Grades::average(10, 10, 9.8);

SPL Exceptionshttp://php.net/spl

Quer saber mais?Acabou de sair do forno.

PSR-3Um padrão de logs para o PHP

Concluindo

Falhando miseravelmente com PHPUma re-interpretação do título.

Falhando miseravelmente com PHPErros e mensagens do sistema DEVEM ser expostos

sempre que possível.

Falhando miseravelmente com PHPExpostos pra todos os desenvolvedores (e administradores)

envolvidos na manutenção do sistema, não para usuários.Muito menos para toda internet.

Falhando miseravelmente com PHPPELO MENOS os gerados pelo próprio PHP.

Dúvidas?ou críticas, sugestões, declarações, conjecturações, etc...

Recommended