You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Placeholder issue where details will need to be added later.
For Serverpod 3.0 we should rework our exception hierarchy in Serverpod to ensure consistency throughout the framework.
One example of a strange exception hierarchy are the specializations for DatabaseInsertRowException, DatabaseUpdateRowException and DatabaseDeleteRowException. These don't really explain what went wrong but more reflect the domain where it went wrong.
Client Exceptions
This rework should also inspect the client exception hierarchy. We should probably have a base class and then all other exceptions on the client should be derived from that.
When refactoring the client-side exception hierarchy, it is necessary to create a dedicated ServerpodClientNetworkError exception to replace the current usage of ServerpodClientException with statusCode: -1 to replace http.ClientException on web and SocketException on native. This pattern already existed for web and was replicated to native on PR #4072.
I think JSON parsing error handling can be more explicit, with two exceptions:
JsonDecodingException: Malformed JSON sent by the server. Sometimes Nginx, Cloudflare, or a reverse proxy may return HTML in case the server is down. There are other cases of why this may happen. Can be handled by catching FormatException
JsonDeserializationException: Even if the response body is valid JSON, models are generated on both server and client side (type-safe, automated), it is still not guaranteed that the JSON structure is compatible with the data class model. Especially if server deployment can be self-hosted (like WordPress).
The app user may use an outdated client
The server admin may use an outdated Server that sends missing required fields to the client.
Can be handled by on TypeError catch (e) (not on Exception catch (e) if we use ! in fromJson methods). (Example)
The issue: There is an inconsistency in the message, i.e., one is 'Unknown server response code. ($e)' and the other is e.toString() directly, which may make parsing the underlying SocketException less straightforward, although at least all use -1 status code.
As @marcelomendoncasoares mentioned, the ideal solution is to create another exception specifically for these failures. In these cases, we don't have HTTP statusCode at all because the connection failed
I think the ideal solution is to not expose statusCode by default, instead use a sealed class, and map SocketException properly in a type-safe way, so statusCode is only available for HTTP client failures. Example.
The proposal:
Remove ServerpodClientException.statusCode
Create ServerpodClientException with statusCode
Update all of ServerpodClientUnauthorized, ServerpodClientForbidden, ServerpodClientBadRequest, ServerpodClientNotFound and ServerpodClientInternalServerError to extend ServerpodClientException.
Create ServerpodClientNetworkException that gets mapped from SocketException/ClientException.
ServerpodClientNetworkError
This Error suffix name can be confused with Dart errors (e.g., ArguemntError), which are programming errors/bugs that typically should not be handled at runtime (or at all):
Activity
marcelomendoncasoares commentedon Oct 20, 2025
When refactoring the client-side exception hierarchy, it is necessary to create a dedicated
ServerpodClientNetworkErrorexception to replace the current usage ofServerpodClientExceptionwithstatusCode: -1to replacehttp.ClientExceptionon web andSocketExceptionon native. This pattern already existed for web and was replicated to native on PR #4072.EchoEllet commentedon May 27, 2026
JSON parsing
I think JSON parsing error handling can be more explicit, with two exceptions:
JsonDecodingException: Malformed JSON sent by the server. Sometimes Nginx, Cloudflare, or a reverse proxy may return HTML in case the server is down. There are other cases of why this may happen. Can be handled by catchingFormatExceptionJsonDeserializationException: Even if the response body is valid JSON, models are generated on both server and client side (type-safe, automated), it is still not guaranteed that the JSON structure is compatible with the data class model. Especially if server deployment can be self-hosted (like WordPress).on TypeError catch (e)(noton Exception catch (e)if we use!infromJsonmethods). (Example)Socket / Network failures
SocketExceptionandClientException, and technically only one of them is needed (according topackage:httpinternals).'Unknown server response code. ($e)'and the other ise.toString()directly, which may make parsing the underlyingSocketExceptionless straightforward, although at least all use-1status code.As @marcelomendoncasoares mentioned, the ideal solution is to create another exception specifically for these failures. In these cases, we don't have HTTP
statusCodeat all because the connection failedI think the ideal solution is to not expose
statusCodeby default, instead use a sealed class, and mapSocketExceptionproperly in a type-safe way, sostatusCodeis only available for HTTP client failures. Example.The proposal:
ServerpodClientException.statusCodeServerpodClientExceptionwithstatusCodeServerpodClientUnauthorized,ServerpodClientForbidden,ServerpodClientBadRequest,ServerpodClientNotFoundandServerpodClientInternalServerErrorto extendServerpodClientException.ServerpodClientNetworkExceptionthat gets mapped fromSocketException/ClientException.This
Errorsuffix name can be confused with Dart errors (e.g.,ArguemntError), which are programming errors/bugs that typically should not be handled at runtime (or at all):https://dart.dev/tools/linter-rules/avoid_catching_errors
I would use
FailureorException