Skip to content

feat: Implement Default Values on Exception model fields - #2678

Merged
SandPod merged 7 commits into
serverpod:mainfrom
klkucaj:feature/1140-on-exception
Aug 28, 2024
Merged

SandPod merged 7 commits into
serverpod:mainfrom
klkucaj:feature/1140-on-exception

Conversation

@klkucaj

@klkucaj klkucaj commented Aug 27, 2024 •

Copy link
Copy Markdown
Contributor

Overview

This is a follow-up to #2383.
This PR introduces support for default values in exception fields.

Default Types

In exception models, there are two types of default values that can be specified:

  • default: Adds a default value to the model field (Dart code).
  • defaultModel: Adds a default value specifically to the model field on the Dart side.

Important Note

Since exceptions are never persisted to the database, we do not support defaultPersist for exception fields. This means that only default and defaultModel are relevant in this context.

However, it’s important to note that defaultModel will override default if both are specified. Since exceptions only exist in the model (Dart code) and are not persisted, using defaultModel in combination with default is redundant. The defaultModel value will always take precedence over default, making it unnecessary to use both in conjunction.

Default Value Options for Exception Fields

In exception models, default values can be assigned to any field type that is supported in class models. This provides flexibility in specifying initial values for various field types, ensuring consistency across your exception handling.

Example Exception YAML

exception: DefaultException
fields:
  intDefault: int, default=10
  boolDefault: bool, default=true
  stringDefault: String, default='Default message'
  dateTimeDefault: DateTime, default=now
  uuidDefault: UuidValue, default=random
  durationDefault: Duration, default=1d 2h 30min
  defaultModelField: String, defaultModel='Model specific message'

Related Issues

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

If you need help, consider asking for advice on the discussion board.

Breaking changes

@klkucaj
klkucaj force-pushed the feature/1140-on-exception branch from e5fce5b to d6ff178 Compare August 27, 2024 09:40

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! change is pretty small.
Still would be nice to see more tests around this. Since, from the user perspective, this is separate feature.

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🚀

@SandPod
SandPod merged commit 54a85c4 into serverpod:main Aug 28, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support default values in exceptions Default value support in protocol / generated code / database default value

2 participants