Browse Source

Type-hinted error handlers, pp. 9-10.

Frederic G. MARAND 7 years ago
parent
commit
78b43c2784
2 changed files with 33 additions and 31 deletions
  1. 22 31
      .idea/workspace.xml
  2. 11 0
      src/controllers.php

+ 22 - 31
.idea/workspace.xml

@@ -16,21 +16,11 @@
   </component>
   <component name="FileEditorManager">
     <leaf>
-      <file leaf-file-name="CallbackResolver.php" pinned="false" current-in-tab="false">
-        <entry file="file://$PROJECT_DIR$/vendor/silex/silex/src/Silex/CallbackResolver.php">
-          <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="320">
-              <caret line="57" column="20" lean-forward="true" selection-start-line="57" selection-start-column="20" selection-end-line="57" selection-end-column="20" />
-              <folding />
-            </state>
-          </provider>
-        </entry>
-      </file>
       <file leaf-file-name="controllers.php" pinned="false" current-in-tab="true">
         <entry file="file://$PROJECT_DIR$/src/controllers.php">
           <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="202">
-              <caret line="74" column="24" lean-forward="true" selection-start-line="74" selection-start-column="24" selection-end-line="74" selection-end-column="24" />
+            <state relative-caret-position="259">
+              <caret line="81" column="46" lean-forward="false" selection-start-line="81" selection-start-column="46" selection-end-line="81" selection-end-column="46" />
               <folding />
             </state>
           </provider>
@@ -42,6 +32,7 @@
     <findStrings>
       <find>welcome to</find>
       <find>resolveCallback</find>
+      <find>httpexcep</find>
     </findStrings>
     <dirStrings>
       <dir>$PROJECT_DIR$/vendor</dir>
@@ -461,12 +452,12 @@
       <workItem from="1511938964059" duration="4476000" />
       <workItem from="1511945660695" duration="9000" />
       <workItem from="1511945681954" duration="1284000" />
-      <workItem from="1511975755546" duration="3418000" />
+      <workItem from="1511975755546" duration="3855000" />
     </task>
     <servers />
   </component>
   <component name="TimeTrackingManager">
-    <option name="totallyTimeSpent" value="9187000" />
+    <option name="totallyTimeSpent" value="9624000" />
   </component>
   <component name="ToolWindowManager">
     <frame x="0" y="23" width="1676" height="1027" extended-state="6" />
@@ -505,16 +496,16 @@
       <breakpoints>
         <line-breakpoint enabled="true" type="php">
           <url>file://$PROJECT_DIR$/src/controllers.php</url>
-          <line>75</line>
+          <line>86</line>
           <option name="timeStamp" value="28" />
         </line-breakpoint>
         <line-breakpoint enabled="true" type="php">
-          <url>file://$PROJECT_DIR$/vendor/silex/silex/src/Silex/CallbackResolver.php</url>
-          <line>49</line>
-          <option name="timeStamp" value="32" />
+          <url>file://$PROJECT_DIR$/src/controllers.php</url>
+          <line>83</line>
+          <option name="timeStamp" value="36" />
         </line-breakpoint>
       </breakpoints>
-      <option name="time" value="33" />
+      <option name="time" value="37" />
     </breakpoint-manager>
     <watches-manager />
   </component>
@@ -809,14 +800,6 @@
         </state>
       </provider>
     </entry>
-    <entry file="file://$PROJECT_DIR$/vendor/silex/silex/src/Silex/ExceptionListenerWrapper.php">
-      <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="297">
-          <caret line="51" column="0" lean-forward="true" selection-start-line="51" selection-start-column="0" selection-end-line="51" selection-end-column="0" />
-          <folding />
-        </state>
-      </provider>
-    </entry>
     <entry file="file://$PROJECT_DIR$/vendor/symfony/http-kernel/Event/KernelEvent.php">
       <provider selected="true" editor-type-id="text-editor">
         <state relative-caret-position="432">
@@ -875,16 +858,24 @@
     </entry>
     <entry file="file://$PROJECT_DIR$/vendor/silex/silex/src/Silex/CallbackResolver.php">
       <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="320">
-          <caret line="57" column="20" lean-forward="true" selection-start-line="57" selection-start-column="20" selection-end-line="57" selection-end-column="20" />
+        <state relative-caret-position="170">
+          <caret line="47" column="18" lean-forward="false" selection-start-line="47" selection-start-column="18" selection-end-line="47" selection-end-column="18" />
+          <folding />
+        </state>
+      </provider>
+    </entry>
+    <entry file="file://$PROJECT_DIR$/vendor/silex/silex/src/Silex/ExceptionListenerWrapper.php">
+      <provider selected="true" editor-type-id="text-editor">
+        <state relative-caret-position="762">
+          <caret line="91" column="0" lean-forward="false" selection-start-line="91" selection-start-column="0" selection-end-line="91" selection-end-column="0" />
           <folding />
         </state>
       </provider>
     </entry>
     <entry file="file://$PROJECT_DIR$/src/controllers.php">
       <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="202">
-          <caret line="74" column="24" lean-forward="true" selection-start-line="74" selection-start-column="24" selection-end-line="74" selection-end-column="24" />
+        <state relative-caret-position="259">
+          <caret line="81" column="46" lean-forward="false" selection-start-line="81" selection-start-column="46" selection-end-line="81" selection-end-column="46" />
           <folding />
         </state>
       </provider>

+ 11 - 0
src/controllers.php

@@ -6,6 +6,7 @@ use demo\Controllers\UserController;
 use Silex\Application;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\Exception\HttpException;
 
 //Request::setTrustedProxies(array('127.0.0.1'));
 
@@ -72,6 +73,16 @@ $app->get('/user/{user}', UserController::class . '::itemAction')
   ->convert('user', 'converter.user:convert');
 
 /* Error handlers receive exactly these arguments, in that order. */
+$app->error(function (HttpException $e, Request $request, $code) {
+  $response = new Response('HTTP Error caught: skipping generic error handler.',
+    // This status is ignored by Silex, which tries to ensure status code
+    // consistency with the exception.
+    Response::HTTP_NOT_FOUND,
+    // But we can force it with this fake header, which is removed before
+    // sending the response to the user agent.
+    ['X-Status-Code' => Response::HTTP_OK]);
+  return $response;
+});
 $app->error(function (\Exception $e, Request $request, $code) use ($app) {
   if ($app['debug']) {
     return;