]> BookStack Code Mirror - bookstack/commitdiff
Improved Exception handling, Removed npm requirement for testing
authorDan Brown <redacted>
Sat, 3 Sep 2016 11:08:58 +0000 (12:08 +0100)
committerDan Brown <redacted>
Sat, 3 Sep 2016 11:08:58 +0000 (12:08 +0100)
.travis.yml
app/Exceptions/Handler.php
app/Exceptions/PrettyException.php
app/Providers/AppServiceProvider.php
app/Services/SocialAuthService.php
app/helpers.php
phpunit.xml

index c4d289a53bdfc09c2e6b1d7d2afdea3c754b9b96..e2eb5f51111bf6a76523f83dc020da1a44152160 100644 (file)
@@ -6,7 +6,6 @@ php:
 
 cache:
   directories:
-    - node_modules
     - $HOME/.composer/cache
 
 addons:
@@ -16,9 +15,6 @@ addons:
     - mysql-client-core-5.6
     - mysql-client-5.6
 
-before_install:
-  - npm install -g npm@latest
-
 before_script:
   - mysql -u root -e 'create database `bookstack-test`;'
   - composer config -g github-oauth.github.com $GITHUB_ACCESS_TOKEN
@@ -26,8 +22,6 @@ before_script:
   - composer self-update
   - composer dump-autoload --no-interaction
   - composer install --prefer-dist --no-interaction
-  - npm install
-  - ./node_modules/.bin/gulp
   - php artisan clear-compiled -n
   - php artisan optimize -n
   - php artisan migrate --force -n --database=mysql_testing
index 40dd1ec105d3c150a6579ffef095001f3e5a959a..57e807db0e14d20014ebf13dea9469cdce87ea43 100644 (file)
@@ -47,19 +47,44 @@ class Handler extends ExceptionHandler
     {
         // Handle notify exceptions which will redirect to the
         // specified location then show a notification message.
-        if ($e instanceof NotifyException) {
-            session()->flash('error', $e->message);
+        if ($this->isExceptionType($e, NotifyException::class)) {
+            session()->flash('error', $this->getOriginalMessage($e));
             return redirect($e->redirectLocation);
         }
 
         // Handle pretty exceptions which will show a friendly application-fitting page
         // Which will include the basic message to point the user roughly to the cause.
-        if (($e instanceof PrettyException || $e->getPrevious() instanceof PrettyException)  && !config('app.debug')) {
-            $message = ($e instanceof PrettyException) ? $e->getMessage() : $e->getPrevious()->getMessage();
+        if ($this->isExceptionType($e, PrettyException::class)  && !config('app.debug')) {
+            $message = $this->getOriginalMessage($e);
             $code = ($e->getCode() === 0) ? 500 : $e->getCode();
             return response()->view('errors/' . $code, ['message' => $message], $code);
         }
 
         return parent::render($request, $e);
     }
+
+    /**
+     * Check the exception chain to compare against the original exception type.
+     * @param Exception $e
+     * @param $type
+     * @return bool
+     */
+    protected function isExceptionType(Exception $e, $type) {
+        do {
+            if (is_a($e, $type)) return true;
+        } while ($e = $e->getPrevious());
+        return false;
+    }
+
+    /**
+     * Get original exception message.
+     * @param Exception $e
+     * @return string
+     */
+    protected function getOriginalMessage(Exception $e) {
+        do {
+            $message = $e->getMessage();
+        } while ($e = $e->getPrevious());
+        return $message;
+    }
 }
index d92acf831f8edf04627fe2dcf3db5e2c830685eb..889252006f4cdb349f88103793035d4fc551f28d 100644 (file)
@@ -1,5 +1,3 @@
 <?php namespace BookStack\Exceptions;
 
-use Exception;
-
-class PrettyException extends Exception {}
\ No newline at end of file
+class PrettyException extends \Exception {}
\ No newline at end of file
index f214c9141aa2b5835de735b8baf8e6f01b0ff6b5..4665bf6c702252b5cb8ac534661f218931aa0f6b 100644 (file)
@@ -1,10 +1,6 @@
-<?php
+<?php namespace BookStack\Providers;
 
-namespace BookStack\Providers;
-
-use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\ServiceProvider;
-use BookStack\User;
 
 class AppServiceProvider extends ServiceProvider
 {
index 7e1bd4246d34404e2b140171796544f3080dc314..b28a97ea43660b7cd3829cd6958e3090e20dde68 100644 (file)
@@ -158,7 +158,7 @@ class SocialAuthService
         $driver = trim(strtolower($socialDriver));
 
         if (!in_array($driver, $this->validSocialDrivers)) abort(404, 'Social Driver Not Found');
-        if (!$this->checkDriverConfigured($driver)) throw new SocialDriverNotConfigured;
+        if (!$this->checkDriverConfigured($driver)) throw new SocialDriverNotConfigured("Your {$driver} social settings are not configured correctly.");
 
         return $driver;
     }
index d28b1956f813e678cd2da116bccc727cea5eacfa..b8abb10066a94d46bb155e2ff07c617281d49f86 100644 (file)
@@ -7,25 +7,32 @@ use BookStack\Ownable;
  *
  * @param  string $file
  * @return string
- *
- * @throws \InvalidArgumentException
+ * @throws Exception
  */
-function versioned_asset($file)
+function versioned_asset($file = '')
 {
-    static $manifest = null;
+    // Don't require css and JS assets for testing
+    if (config('app.env') === 'testing') return '';
 
-    if (is_null($manifest)) {
-        $manifest = json_decode(file_get_contents(public_path('build/manifest.json')), true);
+    static $manifest = null;
+    $manifestPath = 'build/manifest.json';
+
+    if (is_null($manifest) && file_exists($manifestPath)) {
+        $manifest = json_decode(file_get_contents(public_path($manifestPath)), true);
+    } else if (!file_exists($manifestPath)) {
+        if (config('app.env') !== 'production') {
+            $path = public_path($manifestPath);
+            $error = "No {$path} file found, Ensure you have built the css/js assets using gulp.";
+        } else {
+            $error = "No {$manifestPath} file found, Ensure you are using the release version of BookStack";
+        }
+        throw new \Exception($error);
     }
 
     if (isset($manifest[$file])) {
         return baseUrl($manifest[$file]);
     }
 
-    if (file_exists(public_path($file))) {
-        return baseUrl($file);
-    }
-
     throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
 }
 
index 2150a5aa387b2b54f54c14bd066f71e2546540f7..a2b26d4132f87c11ba63d470009ae15d6f559eaf 100644 (file)
@@ -28,7 +28,7 @@
         <env name="DB_CONNECTION" value="mysql_testing"/>
         <env name="MAIL_DRIVER" value="log"/>
         <env name="AUTH_METHOD" value="standard"/>
-        <env name="DISABLE_EXTERNAL_SERVICES" value="false"/>
+        <env name="DISABLE_EXTERNAL_SERVICES" value="true"/>
         <env name="LDAP_VERSION" value="3"/>
         <env name="GITHUB_APP_ID" value="aaaaaaaaaaaaaa"/>
         <env name="GITHUB_APP_SECRET" value="aaaaaaaaaaaaaa"/>