Skip to content

Commit

Permalink
fix(forms): Convert existing reactive errors to use RuntimeErrorCode. (
Browse files Browse the repository at this point in the history
…#46560)

This allows for better tree-shakability, as well as the addition of guides in the future as needed.

PR Close #46560
  • Loading branch information
dylhunn committed Jun 29, 2022
1 parent e405eca commit 74a26d8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
10 changes: 10 additions & 0 deletions goldens/public-api/forms/errors.md
Expand Up @@ -6,6 +6,16 @@

// @public
export const enum RuntimeErrorCode {
// (undocumented)
FORM_ARRAY_NAME_MISSING_PARENT = 1054,
// (undocumented)
FORM_CONTROL_NAME_INSIDE_MODEL_GROUP = 1051,
// (undocumented)
FORM_CONTROL_NAME_MISSING_PARENT = 1050,
// (undocumented)
FORM_GROUP_MISSING_INSTANCE = 1052,
// (undocumented)
FORM_GROUP_NAME_MISSING_PARENT = 1053,
// (undocumented)
MISSING_CONTROL = 1001,
// (undocumented)
Expand Down
20 changes: 15 additions & 5 deletions packages/forms/src/directives/reactive_errors.ts
Expand Up @@ -6,11 +6,16 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ɵRuntimeError as RuntimeError} from '@angular/core';

import {RuntimeErrorCode} from '../errors';

import {formArrayNameExample, formControlNameExample, formGroupNameExample, ngModelGroupExample} from './error_examples';


export function controlParentException(): Error {
return new Error(
return new RuntimeError(
RuntimeErrorCode.FORM_CONTROL_NAME_MISSING_PARENT,
`formControlName must be used with a parent formGroup directive. You'll want to add a formGroup
directive and pass it an existing FormGroup instance (you can create one in your class).
Expand All @@ -20,7 +25,8 @@ export function controlParentException(): Error {
}

export function ngModelGroupException(): Error {
return new Error(
return new RuntimeError(
RuntimeErrorCode.FORM_CONTROL_NAME_INSIDE_MODEL_GROUP,
`formControlName cannot be used with an ngModelGroup parent. It is only compatible with parents
that also have a "form" prefix: formGroupName, formArrayName, or formGroup.
Expand All @@ -34,15 +40,18 @@ export function ngModelGroupException(): Error {
}

export function missingFormException(): Error {
return new Error(`formGroup expects a FormGroup instance. Please pass one in.
return new RuntimeError(
RuntimeErrorCode.FORM_GROUP_MISSING_INSTANCE,
`formGroup expects a FormGroup instance. Please pass one in.
Example:
${formControlNameExample}`);
}

export function groupParentException(): Error {
return new Error(
return new RuntimeError(
RuntimeErrorCode.FORM_GROUP_NAME_MISSING_PARENT,
`formGroupName must be used with a parent formGroup directive. You'll want to add a formGroup
directive and pass it an existing FormGroup instance (you can create one in your class).
Expand All @@ -52,7 +61,8 @@ export function groupParentException(): Error {
}

export function arrayParentException(): Error {
return new Error(
return new RuntimeError(
RuntimeErrorCode.FORM_ARRAY_NAME_MISSING_PARENT,
`formArrayName must be used with a parent formGroup directive. You'll want to add a formGroup
directive and pass it an existing FormGroup instance (you can create one in your class).
Expand Down
6 changes: 6 additions & 0 deletions packages/forms/src/errors.ts
Expand Up @@ -18,6 +18,12 @@ export const enum RuntimeErrorCode {
MISSING_CONTROL = 1001,
MISSING_CONTROL_VALUE = 1002,

FORM_CONTROL_NAME_MISSING_PARENT = 1050,
FORM_CONTROL_NAME_INSIDE_MODEL_GROUP = 1051,
FORM_GROUP_MISSING_INSTANCE = 1052,
FORM_GROUP_NAME_MISSING_PARENT = 1053,
FORM_ARRAY_NAME_MISSING_PARENT = 1054,

// Validators errors
WRONG_VALIDATOR_RETURN_TYPE = -1101,

Expand Down

0 comments on commit 74a26d8

Please sign in to comment.