Skip to content

Rework exception hierarchy in Serverpod. #3283

Closed

Description

@SandPod

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.

Activity

  1. added
    area: database
    breaking
    on Mar 4, 2025
  2. added this to the 3.0 Release milestone on Sep 18, 2025
  3. added
    area: client
    on Oct 20, 2025
  4. marcelomendoncasoares commented on Oct 20, 2025

    @marcelomendoncasoares
    Collaborator

    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.

  5. removed this from the 3.0 Release milestone on Oct 24, 2025
  6. EchoEllet commented on May 27, 2026

    @EchoEllet
    Contributor

    Client Exceptions

    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 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)

    Socket / Network failures

    • #5134 handles both SocketException and ClientException, and technically only one of them is needed (according to package:http internals).
      • 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:

    1. Remove ServerpodClientException.statusCode
    2. Create ServerpodClientException with statusCode
    • Update all of ServerpodClientUnauthorized, ServerpodClientForbidden, ServerpodClientBadRequest, ServerpodClientNotFound and ServerpodClientInternalServerError to extend ServerpodClientException.
    1. 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):

    https://dart.dev/tools/linter-rules/avoid_catching_errors

    I would use Failure or Exception

  7. moved this from Backlog 🗂️ to Planned 🔭 in Serverpod Frameworkson Jul 10, 2026
  8. moved this from Planned 🔭 to Next Up 🎯 in Serverpod Frameworkson Jul 27, 2026
  9. moved this from Next Up 🎯 to In Progress 🔥 in Serverpod Frameworkson Aug 6, 2026
  10. moved this from In Progress 🔥 to Done 🚀 in Serverpod Frameworkson Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: clientarea: databasebreaking

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions