1010
1111use Inhere \Console \AbstractApplication ;
1212use Inhere \Console \Contract \ErrorHandlerInterface ;
13+ use Inhere \Console \Exception \PromptException ;
1314use Throwable ;
1415use Toolkit \Cli \Highlighter ;
1516use function file_get_contents ;
@@ -29,20 +30,26 @@ class ErrorHandler implements ErrorHandlerInterface
2930 */
3031 public function handle (Throwable $ e , AbstractApplication $ app ): void
3132 {
33+ if ($ e instanceof PromptException) {
34+ $ app ->getOutput ()->error ($ e ->getMessage ());
35+ return ;
36+ }
37+
3238 $ class = get_class ($ e );
3339
3440 // open debug, throw exception
3541 if ($ app ->isDebug ()) {
36- $ tpl = <<<ERR
42+ $ tpl = <<<ERR
3743\n<error> Error </error> <mga>%s</mga>
3844
3945At File <cyan>%s</cyan> line <bold>%d</bold>
4046Exception class is <magenta> $ class</magenta>
4147<comment>Code View:</comment> \n\n%s
4248<comment>Code Trace:</comment> \n\n%s \n
4349ERR ;
44- $ line = $ e ->getLine ();
45- $ file = $ e ->getFile ();
50+ $ line = $ e ->getLine ();
51+ $ file = $ e ->getFile ();
52+
4653 $ snippet = Highlighter::create ()->highlightSnippet (file_get_contents ($ file ), $ line , 3 , 3 );
4754 $ message = sprintf (
4855 $ tpl , // $e->getCode(),
@@ -58,10 +65,11 @@ public function handle(Throwable $e, AbstractApplication $app): void
5865 }
5966
6067 $ app ->write ($ message , false );
61- } else {
62- // simple output
63- $ app ->getOutput ()->error ('An error occurred! MESSAGE: ' . $ e ->getMessage ());
64- $ app ->write ("\nYou can use '--debug 4' to see error details. " );
68+ return ;
6569 }
70+
71+ // simple output
72+ $ app ->getOutput ()->error ('An error occurred! MESSAGE: ' . $ e ->getMessage ());
73+ $ app ->write ("\nYou can use '--debug 4' to see error details. " );
6674 }
6775}
0 commit comments