Falhando miseralvelmente com PHP

102
Falhando miseravelmente com PHP Augusto Pascutti

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.

Transcript of Falhando miseralvelmente com PHP

Page 1: Falhando miseralvelmente com PHP

Falhando miseravelmente com PHPAugusto Pascutti

Page 2: Falhando miseralvelmente com PHP

50min para resolver:

Page 3: Falhando miseralvelmente com PHP

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

Usuário do sistema

Page 4: Falhando miseralvelmente com PHP

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

Desenvolvedor querendo fazer merda

Page 5: Falhando miseralvelmente com PHP

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

Usuário do sistema

Page 6: Falhando miseralvelmente com PHP

Eu entro e dá erro.Usuários

Page 7: Falhando miseralvelmente com PHP

50 minutos sobre:

Erros e outras mensagens relevantes

Utilidade dessas mensagens

Geração dessas mensagens

Page 8: Falhando miseralvelmente com PHP

Errose outras mensagens relevantes

Page 9: Falhando miseralvelmente com PHP

Since the beginning, life has relied upon the transmission of

messages.RFC 3164:The BSD syslog Protocol

Page 10: Falhando miseralvelmente com PHP

SeveridadesTipos de mensagens relevantes

Page 11: Falhando miseralvelmente com PHP

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

Page 12: Falhando miseralvelmente com PHP

Severidades precisam de um contexto

Eu mesmo, agora há pouco.

Page 13: Falhando miseralvelmente com PHP

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

Page 14: Falhando miseralvelmente com PHP

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

Page 15: Falhando miseralvelmente com PHP

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

Page 16: Falhando miseralvelmente com PHP

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

Page 17: Falhando miseralvelmente com PHP

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

Page 18: Falhando miseralvelmente com PHP

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

versões futuras.

Page 19: Falhando miseralvelmente com PHP

E_STRICTSugestões para melhor interoperabilidade e

compatibilidade futura.

Page 20: Falhando miseralvelmente com PHP

Utilidadedessas mensagens

Page 21: Falhando miseralvelmente com PHP

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

mais atenção você presta.Newton

Page 22: Falhando miseralvelmente com PHP

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; [email protected])"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;

Page 23: Falhando miseralvelmente com PHP

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

Page 24: Falhando miseralvelmente com PHP

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

Algum desenvolvedor

Page 25: Falhando miseralvelmente com PHP

Como usar isso?O (meu) jeito *nix

Page 26: Falhando miseralvelmente com PHP
Page 27: Falhando miseralvelmente com PHP

word, line, character, and byte count

wc

Page 28: Falhando miseralvelmente com PHP

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

Page 29: Falhando miseralvelmente com PHP

print lines matching a pattern

grep

Page 30: Falhando miseralvelmente com PHP

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

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

Page 31: Falhando miseralvelmente com PHP

#!/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

Page 32: Falhando miseralvelmente com PHP

#!/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"

Page 33: Falhando miseralvelmente com PHP

#!/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"

Page 34: Falhando miseralvelmente com PHP
Page 35: Falhando miseralvelmente com PHP

Procura isso no Analytics agora.Augusto Pascutti

Page 36: Falhando miseralvelmente com PHP

display first lines of a file

head

Page 37: Falhando miseralvelmente com PHP

#!/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"

Page 38: Falhando miseralvelmente com PHP

pattern-directed scanning and processing language

awk

Page 39: Falhando miseralvelmente com PHP

#!/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

Page 40: Falhando miseralvelmente com PHP

#!/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

Page 41: Falhando miseralvelmente com PHP

sort lines of text files

sort

Page 42: Falhando miseralvelmente com PHP

#!/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

Page 43: Falhando miseralvelmente com PHP

#!/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

Page 44: Falhando miseralvelmente com PHP

report or filter out repeated lines in a file

uniq

Page 45: Falhando miseralvelmente com PHP

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

Page 46: Falhando miseralvelmente com PHP

#!/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

Page 47: Falhando miseralvelmente com PHP

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

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

Page 48: Falhando miseralvelmente com PHP

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

Eu

Page 49: Falhando miseralvelmente com PHP

#!/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;

Page 50: Falhando miseralvelmente com PHP

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

Page 51: Falhando miseralvelmente com PHP

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

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

Page 52: Falhando miseralvelmente com PHP
Page 53: Falhando miseralvelmente com PHP

Top 10

Page 54: Falhando miseralvelmente com PHP

#!/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

Page 55: Falhando miseralvelmente com PHP

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

Page 56: Falhando miseralvelmente com PHP

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

Page 57: Falhando miseralvelmente com PHP

Log do PHPVia linha de comando.

Page 58: Falhando miseralvelmente com PHP

[/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 ()]

Page 59: Falhando miseralvelmente com PHP

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

Page 60: Falhando miseralvelmente com PHP

#!/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)]

Page 61: Falhando miseralvelmente com PHP
Page 62: Falhando miseralvelmente com PHP
Page 63: Falhando miseralvelmente com PHP
Page 64: Falhando miseralvelmente com PHP

Geraçãodessas mensagens

Page 65: Falhando miseralvelmente com PHP
Page 66: Falhando miseralvelmente com PHP

ConfiguraçõesOu coisas que podem provocar diferentes resultados

no seu PHP.

Page 67: Falhando miseralvelmente com PHP

display_errorsVocê quer ver esses erros?

Page 68: Falhando miseralvelmente com PHP

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

(Ideal para ambiente de desenvolvimento)

Page 69: Falhando miseralvelmente com PHP
Page 70: Falhando miseralvelmente com PHP

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

(Ideal para ambiente de produção)

Page 71: Falhando miseralvelmente com PHP
Page 72: Falhando miseralvelmente com PHP

log_errorsEnvia os erros para o arquivo de log configurado.

Page 73: Falhando miseralvelmente com PHP

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

Page 74: Falhando miseralvelmente com PHP

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

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

Page 75: Falhando miseralvelmente com PHP

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

Page 76: Falhando miseralvelmente com PHP

error_reportingQuais severidades utilizar?

Page 77: Falhando miseralvelmente com PHP

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 */

Page 78: Falhando miseralvelmente com PHP

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 */

Page 79: Falhando miseralvelmente com PHP

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 */

Page 80: Falhando miseralvelmente com PHP

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 */

Page 81: Falhando miseralvelmente com PHP

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 */

Page 82: Falhando miseralvelmente com PHP

Procedural

Page 83: Falhando miseralvelmente com PHP

trigger_error()Gera uma mensagem de determinada severidade.

Page 84: Falhando miseralvelmente com PHP

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

Page 85: Falhando miseralvelmente com PHP

error_log()Envia mensagem para o log de erros.

Page 86: Falhando miseralvelmente com PHP

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, '[email protected]');

Page 87: Falhando miseralvelmente com PHP

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, '[email protected]');

Page 88: Falhando miseralvelmente com PHP
Page 89: Falhando miseralvelmente com PHP

ExceptionsErros num paradigma Orientado a Objetos

Page 90: Falhando miseralvelmente com PHP

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);

Page 91: Falhando miseralvelmente com PHP
Page 92: Falhando miseralvelmente com PHP

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);

Page 93: Falhando miseralvelmente com PHP
Page 94: Falhando miseralvelmente com PHP

SPL Exceptionshttp://php.net/spl

Page 95: Falhando miseralvelmente com PHP

Quer saber mais?Acabou de sair do forno.

Page 96: Falhando miseralvelmente com PHP

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

Page 97: Falhando miseralvelmente com PHP

Concluindo

Page 98: Falhando miseralvelmente com PHP

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

Page 99: Falhando miseralvelmente com PHP

Falhando miseravelmente com PHPErros e mensagens do sistema DEVEM ser expostos

sempre que possível.

Page 100: Falhando miseralvelmente com PHP

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.

Page 101: Falhando miseralvelmente com PHP

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

Page 102: Falhando miseralvelmente com PHP

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