Improved Exception handling, Removed npm requirement for testing
Showing
7 changed files
with
50 additions
and
30 deletions
| ... | @@ -6,7 +6,6 @@ php: | ... | @@ -6,7 +6,6 @@ php: |
| 6 | 6 | ||
| 7 | cache: | 7 | cache: |
| 8 | directories: | 8 | directories: |
| 9 | - - node_modules | ||
| 10 | - $HOME/.composer/cache | 9 | - $HOME/.composer/cache |
| 11 | 10 | ||
| 12 | addons: | 11 | addons: |
| ... | @@ -16,9 +15,6 @@ addons: | ... | @@ -16,9 +15,6 @@ addons: |
| 16 | - mysql-client-core-5.6 | 15 | - mysql-client-core-5.6 |
| 17 | - mysql-client-5.6 | 16 | - mysql-client-5.6 |
| 18 | 17 | ||
| 19 | -before_install: | ||
| 20 | - - npm install -g npm@latest | ||
| 21 | - | ||
| 22 | before_script: | 18 | before_script: |
| 23 | - mysql -u root -e 'create database `bookstack-test`;' | 19 | - mysql -u root -e 'create database `bookstack-test`;' |
| 24 | - composer config -g github-oauth.github.com $GITHUB_ACCESS_TOKEN | 20 | - composer config -g github-oauth.github.com $GITHUB_ACCESS_TOKEN |
| ... | @@ -26,8 +22,6 @@ before_script: | ... | @@ -26,8 +22,6 @@ before_script: |
| 26 | - composer self-update | 22 | - composer self-update |
| 27 | - composer dump-autoload --no-interaction | 23 | - composer dump-autoload --no-interaction |
| 28 | - composer install --prefer-dist --no-interaction | 24 | - composer install --prefer-dist --no-interaction |
| 29 | - - npm install | ||
| 30 | - - ./node_modules/.bin/gulp | ||
| 31 | - php artisan clear-compiled -n | 25 | - php artisan clear-compiled -n |
| 32 | - php artisan optimize -n | 26 | - php artisan optimize -n |
| 33 | - php artisan migrate --force -n --database=mysql_testing | 27 | - php artisan migrate --force -n --database=mysql_testing | ... | ... |
| ... | @@ -47,19 +47,44 @@ class Handler extends ExceptionHandler | ... | @@ -47,19 +47,44 @@ class Handler extends ExceptionHandler |
| 47 | { | 47 | { |
| 48 | // Handle notify exceptions which will redirect to the | 48 | // Handle notify exceptions which will redirect to the |
| 49 | // specified location then show a notification message. | 49 | // specified location then show a notification message. |
| 50 | - if ($e instanceof NotifyException) { | 50 | + if ($this->isExceptionType($e, NotifyException::class)) { |
| 51 | - session()->flash('error', $e->message); | 51 | + session()->flash('error', $this->getOriginalMessage($e)); |
| 52 | return redirect($e->redirectLocation); | 52 | return redirect($e->redirectLocation); |
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | // Handle pretty exceptions which will show a friendly application-fitting page | 55 | // Handle pretty exceptions which will show a friendly application-fitting page |
| 56 | // Which will include the basic message to point the user roughly to the cause. | 56 | // Which will include the basic message to point the user roughly to the cause. |
| 57 | - if (($e instanceof PrettyException || $e->getPrevious() instanceof PrettyException) && !config('app.debug')) { | 57 | + if ($this->isExceptionType($e, PrettyException::class) && !config('app.debug')) { |
| 58 | - $message = ($e instanceof PrettyException) ? $e->getMessage() : $e->getPrevious()->getMessage(); | 58 | + $message = $this->getOriginalMessage($e); |
| 59 | $code = ($e->getCode() === 0) ? 500 : $e->getCode(); | 59 | $code = ($e->getCode() === 0) ? 500 : $e->getCode(); |
| 60 | return response()->view('errors/' . $code, ['message' => $message], $code); | 60 | return response()->view('errors/' . $code, ['message' => $message], $code); |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | return parent::render($request, $e); | 63 | return parent::render($request, $e); |
| 64 | } | 64 | } |
| 65 | + | ||
| 66 | + /** | ||
| 67 | + * Check the exception chain to compare against the original exception type. | ||
| 68 | + * @param Exception $e | ||
| 69 | + * @param $type | ||
| 70 | + * @return bool | ||
| 71 | + */ | ||
| 72 | + protected function isExceptionType(Exception $e, $type) { | ||
| 73 | + do { | ||
| 74 | + if (is_a($e, $type)) return true; | ||
| 75 | + } while ($e = $e->getPrevious()); | ||
| 76 | + return false; | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + /** | ||
| 80 | + * Get original exception message. | ||
| 81 | + * @param Exception $e | ||
| 82 | + * @return string | ||
| 83 | + */ | ||
| 84 | + protected function getOriginalMessage(Exception $e) { | ||
| 85 | + do { | ||
| 86 | + $message = $e->getMessage(); | ||
| 87 | + } while ($e = $e->getPrevious()); | ||
| 88 | + return $message; | ||
| 89 | + } | ||
| 65 | } | 90 | } | ... | ... |
| 1 | <?php namespace BookStack\Exceptions; | 1 | <?php namespace BookStack\Exceptions; |
| 2 | 2 | ||
| 3 | -use Exception; | ||
| 4 | - | ||
| 5 | -class PrettyException extends Exception {} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 3 | +class PrettyException extends \Exception {} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| 1 | -<?php | 1 | +<?php namespace BookStack\Providers; |
| 2 | 2 | ||
| 3 | -namespace BookStack\Providers; | ||
| 4 | - | ||
| 5 | -use Illuminate\Support\Facades\Auth; | ||
| 6 | use Illuminate\Support\ServiceProvider; | 3 | use Illuminate\Support\ServiceProvider; |
| 7 | -use BookStack\User; | ||
| 8 | 4 | ||
| 9 | class AppServiceProvider extends ServiceProvider | 5 | class AppServiceProvider extends ServiceProvider |
| 10 | { | 6 | { | ... | ... |
| ... | @@ -158,7 +158,7 @@ class SocialAuthService | ... | @@ -158,7 +158,7 @@ class SocialAuthService |
| 158 | $driver = trim(strtolower($socialDriver)); | 158 | $driver = trim(strtolower($socialDriver)); |
| 159 | 159 | ||
| 160 | if (!in_array($driver, $this->validSocialDrivers)) abort(404, 'Social Driver Not Found'); | 160 | if (!in_array($driver, $this->validSocialDrivers)) abort(404, 'Social Driver Not Found'); |
| 161 | - if (!$this->checkDriverConfigured($driver)) throw new SocialDriverNotConfigured; | 161 | + if (!$this->checkDriverConfigured($driver)) throw new SocialDriverNotConfigured("Your {$driver} social settings are not configured correctly."); |
| 162 | 162 | ||
| 163 | return $driver; | 163 | return $driver; |
| 164 | } | 164 | } | ... | ... |
| ... | @@ -7,25 +7,32 @@ use BookStack\Ownable; | ... | @@ -7,25 +7,32 @@ use BookStack\Ownable; |
| 7 | * | 7 | * |
| 8 | * @param string $file | 8 | * @param string $file |
| 9 | * @return string | 9 | * @return string |
| 10 | - * | 10 | + * @throws Exception |
| 11 | - * @throws \InvalidArgumentException | ||
| 12 | */ | 11 | */ |
| 13 | -function versioned_asset($file) | 12 | +function versioned_asset($file = '') |
| 14 | { | 13 | { |
| 15 | - static $manifest = null; | 14 | + // Don't require css and JS assets for testing |
| 15 | + if (config('app.env') === 'testing') return ''; | ||
| 16 | 16 | ||
| 17 | - if (is_null($manifest)) { | 17 | + static $manifest = null; |
| 18 | - $manifest = json_decode(file_get_contents(public_path('build/manifest.json')), true); | 18 | + $manifestPath = 'build/manifest.json'; |
| 19 | + | ||
| 20 | + if (is_null($manifest) && file_exists($manifestPath)) { | ||
| 21 | + $manifest = json_decode(file_get_contents(public_path($manifestPath)), true); | ||
| 22 | + } else if (!file_exists($manifestPath)) { | ||
| 23 | + if (config('app.env') !== 'production') { | ||
| 24 | + $path = public_path($manifestPath); | ||
| 25 | + $error = "No {$path} file found, Ensure you have built the css/js assets using gulp."; | ||
| 26 | + } else { | ||
| 27 | + $error = "No {$manifestPath} file found, Ensure you are using the release version of BookStack"; | ||
| 28 | + } | ||
| 29 | + throw new \Exception($error); | ||
| 19 | } | 30 | } |
| 20 | 31 | ||
| 21 | if (isset($manifest[$file])) { | 32 | if (isset($manifest[$file])) { |
| 22 | return baseUrl($manifest[$file]); | 33 | return baseUrl($manifest[$file]); |
| 23 | } | 34 | } |
| 24 | 35 | ||
| 25 | - if (file_exists(public_path($file))) { | ||
| 26 | - return baseUrl($file); | ||
| 27 | - } | ||
| 28 | - | ||
| 29 | throw new InvalidArgumentException("File {$file} not defined in asset manifest."); | 36 | throw new InvalidArgumentException("File {$file} not defined in asset manifest."); |
| 30 | } | 37 | } |
| 31 | 38 | ... | ... |
| ... | @@ -28,7 +28,7 @@ | ... | @@ -28,7 +28,7 @@ |
| 28 | <env name="DB_CONNECTION" value="mysql_testing"/> | 28 | <env name="DB_CONNECTION" value="mysql_testing"/> |
| 29 | <env name="MAIL_DRIVER" value="log"/> | 29 | <env name="MAIL_DRIVER" value="log"/> |
| 30 | <env name="AUTH_METHOD" value="standard"/> | 30 | <env name="AUTH_METHOD" value="standard"/> |
| 31 | - <env name="DISABLE_EXTERNAL_SERVICES" value="false"/> | 31 | + <env name="DISABLE_EXTERNAL_SERVICES" value="true"/> |
| 32 | <env name="LDAP_VERSION" value="3"/> | 32 | <env name="LDAP_VERSION" value="3"/> |
| 33 | <env name="GITHUB_APP_ID" value="aaaaaaaaaaaaaa"/> | 33 | <env name="GITHUB_APP_ID" value="aaaaaaaaaaaaaa"/> |
| 34 | <env name="GITHUB_APP_SECRET" value="aaaaaaaaaaaaaa"/> | 34 | <env name="GITHUB_APP_SECRET" value="aaaaaaaaaaaaaa"/> | ... | ... |
-
Please register or sign in to post a comment