เดิมการแสดง error จะแสดงออกมาเป็น html แต่เมื่อเรานำ CI มาใช้เป็นระบบ API ถ้าแสดงแบบเดิมจะเกิดปัญหา app บน iphone / ipad ล่ม เพราะว่าข้อมูลที่ส่งออกไป กับที่ ios ทั้งหลายคาดว่าจะได้รับไม่ตรงกัน แก้ไม่ยากครับ แค่แทนที่ไฟล์ใน application/errors จาก code ด้านล่าง
error_404.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php global $application_folder ; // Get an article from the database, show a 404 page if the requested article was not found. $article = get_content( $this ->uri->uri_string()); if ( empty ( $article )){ show_404( $this ->uri->uri_string()); } $datas [ 'status' ] = -1; $datas [ 'type' ] = '404' ; $datas [ 'messageHeading' ] = '404' ; $datas [ 'message' ] = 'file not found' ; $logs = "nnn" . date ( 'Y-m-d H:i:s' ); $logs = $logs . "ntURLt" . $_SERVER [ 'SERVER_ADDR' ]. $_SERVER [ 'REQUEST_URI' ]; $logs = $logs . "ntPOSTt" .print_r( $_POST ,true); $logs = $logs . "ntRETURNt" .print_r( $datas ,true); $fp = fopen ( $application_folder . '/logs/error' . date ( 'Y-m-d' ). '.txt' , 'a+' ); flock ( $fp ,LOCK_EX); fwrite( $fp , $logs ); flock ( $fp ,LOCK_UN); fclose( $fp ); exit (json_encode( $datas )); |
error_db.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php global $application_folder ; $datas [ 'status' ] = -1; $datas [ 'code' ] = 1; $datas [ 'type' ] = 'database' ; $datas [ 'messageHeading' ] = $heading ; $datas [ 'message' ] = 'database error' ; $datas [ 'message' ] = $message ; $logs = "nnn" . date ( 'Y-m-d H:i:s' ); $logs = $logs . "ntURLt" . $_SERVER [ 'SERVER_ADDR' ]. $_SERVER [ 'REQUEST_URI' ]; $logs = $logs . "ntPOSTt" .print_r( $_POST ,true); $logs = $logs . "ntRETURNt" .print_r( $datas ,true); $fp = fopen ( $application_folder . '/logs/error' . date ( 'Y-m-d' ). '.txt' , 'a+' ); flock ( $fp ,LOCK_EX); fwrite( $fp , $logs ); flock ( $fp ,LOCK_UN); fclose( $fp ); exit (json_encode( $datas )); |
error_general.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php global $application_folder ; $datas [ 'status' ] = -1; $datas [ 'type' ] = 'general' ; $datas [ 'messageHeading' ] = $heading ; $datas [ 'message' ] = $message ; $logs = "nnn" . date ( 'Y-m-d H:i:s' ); $logs = $logs . "ntURLt" . $_SERVER [ 'SERVER_ADDR' ]. $_SERVER [ 'REQUEST_URI' ]; $logs = $logs . "ntPOSTt" .print_r( $_POST ,true); $logs = $logs . "ntRETURNt" .print_r( $datas ,true); $fp = fopen ( $application_folder . '/logs/error' . date ( 'Y-m-d' ). '.txt' , 'a+' ); flock ( $fp ,LOCK_EX); fwrite( $fp , $logs ); flock ( $fp ,LOCK_UN); fclose( $fp ); exit (json_encode( $datas )); |
error_php.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php global $application_folder ; $datas [ 'status' ] = -1; $datas [ 'type' ] = 'php' ; $datas [ 'messageHeading' ] = 'A PHP Error was encountered' ; $datas [ 'message' ] = $message ; $datas [ 'severity' ] = $severity ; $datas [ 'filepath' ] = $filepath ; $datas [ 'line' ] = $line ; $logs = "nnn" . date ( 'Y-m-d H:i:s' ); $logs = $logs . "ntURLt" . $_SERVER [ 'SERVER_ADDR' ]. $_SERVER [ 'REQUEST_URI' ]; $logs = $logs . "ntPOSTt" .print_r( $_POST ,true); $logs = $logs . "ntRETURNt" .print_r( $datas ,true); $fp = fopen ( $application_folder . '/logs/error' . date ( 'Y-m-d' ). '.txt' , 'a+' ); flock ( $fp ,LOCK_EX); fwrite( $fp , $logs ); flock ( $fp ,LOCK_UN); fclose( $fp ); exit (json_encode( $datas )); |
เท่านี้ error ทั้งหลายจะแปลงกายไปเป็น แบบ json และบันทึกลงไฟล์ใน application/logs เป็นรายวัน
About the author