# Code Smell 166 - Low-Level Errors on User Interface

> TL;DR: Catch your errors. Even the ones you don't expect.

# Problems

- Security

- Error Handling

- Error Logging

- Bad UX Experience

# Solutions

1. Use a top-level handler

2. Avoid languages favoring [return codes](https://maximilianocontieri.com/code-smell-72-return-codes)

3. Expect database and low-level errors

# Context

Even in 2022, we can see "serious" websites showing casual users a stack or debugging message.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/8d71bdae68fc52a1b6d1c65e8ce944af)
```php
<?

Fatal error: Uncaught Error: Class 'MyClass' not found
 in /nstest/src/Container.php:9
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/3d16a49a680234811e19a56fd3d5be17)
```php
<?

// A user-defined exception handler function
function myException($exception) {
    logError($exception->description())
    // We don't show Exception to final users      
}

// Set user-defined exception handler function
set_exception_handler("myException");
```

# Detection

[X] Automatic 

We can use mutation testing to simulate problems and see if they are handled correctly.

# Tags

- Security

# Conclusion

We need to keep maturing. 

Our solutions shouldn't be sloppy.

We need to improve our reputation as serious software engineers.

# Relations

%[https://maximilianocontieri.com/code-smell-72-return-codes]

# More Info

%[https://maximilianocontieri.com/fail-fast]

# Disclaimer

Code Smells are just my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [jesse orrico](https://unsplash.com/@jessedo81) on [Unsplash](https://unsplash.com/s/photos/dirty)  

* * *

> 80 percent of my problems are simple logic errors. 80 percent of the remaining problems are pointer errors. The remaining problems are hard.

_Mark Donner_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
