Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/aws-cdk-lib/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const enableNoThrowDefaultErrorIn = [
'aws-elasticloadbalancingv2-actions',
'aws-elasticloadbalancingv2-targets',
'aws-lambda',
'aws-logs',
'aws-rds',
'aws-s3',
'aws-sns',
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-logs/lib/data-protection-policy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Construct } from 'constructs';
import { ILogGroup } from './log-group';
import { IBucket } from '../../aws-s3';
import { Stack } from '../../core';
import { Stack, UnscopedValidationError } from '../../core';
/**
* Creates a data protection policy for CloudWatch Logs log groups.
*/
Expand All @@ -10,7 +10,7 @@ export class DataProtectionPolicy {

constructor(props: DataProtectionPolicyProps) {
if (props.identifiers.length == 0) {
throw new Error('DataIdentifier cannot be empty');
throw new UnscopedValidationError('DataIdentifier cannot be empty');
}
this.dataProtectionPolicyProps = props;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/aws-cdk-lib/aws-logs/lib/field-index-policy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Construct } from 'constructs';
import { UnscopedValidationError } from '../../core';

/**
* Creates a field index policy for CloudWatch Logs log groups.
Expand All @@ -8,7 +9,7 @@ export class FieldIndexPolicy {

constructor(props: FieldIndexPolicyProps) {
if (props.fields.length > 20) {
throw new Error('A maximum of 20 fields can be indexed per log group');
throw new UnscopedValidationError('A maximum of 20 fields can be indexed per log group');
}
this.fieldIndexPolicyProps = props;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-logs/lib/log-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ILogSubscriptionDestination, SubscriptionFilter } from './subscription-
import * as cloudwatch from '../../aws-cloudwatch';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import { Annotations, Arn, ArnFormat, RemovalPolicy, Resource, Stack, Token } from '../../core';
import { Annotations, Arn, ArnFormat, RemovalPolicy, Resource, Stack, Token, ValidationError } from '../../core';
import { addConstructMetadata } from '../../core/lib/metadata-resource';

export interface ILogGroup extends iam.IResourceWithPolicy {
Expand Down Expand Up @@ -623,7 +623,7 @@ export class LogGroup extends LogGroupBase {
if (retentionInDays === Infinity || retentionInDays === RetentionDays.INFINITE) { retentionInDays = undefined; }

if (retentionInDays !== undefined && !Token.isUnresolved(retentionInDays) && retentionInDays <= 0) {
throw new Error(`retentionInDays must be positive, got ${retentionInDays}`);
throw new ValidationError(`retentionInDays must be positive, got ${retentionInDays}`, this);
}

let logGroupClass = props.logGroupClass;
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-logs/lib/metric-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Construct } from 'constructs';
import { ILogGroup, MetricFilterOptions } from './log-group';
import { CfnMetricFilter } from './logs.generated';
import { Metric, MetricOptions } from '../../aws-cloudwatch';
import { Resource } from '../../core';
import { Resource, ValidationError } from '../../core';
import { addConstructMetadata, MethodMetadata } from '../../core/lib/metadata-resource';

/**
Expand Down Expand Up @@ -34,7 +34,7 @@ export class MetricFilter extends Resource {

const numberOfDimensions = Object.keys(props.dimensions ?? {}).length;
if (numberOfDimensions > 3) {
throw new Error(`MetricFilter only supports a maximum of 3 dimensions but received ${numberOfDimensions}.`);
throw new ValidationError(`MetricFilter only supports a maximum of 3 dimensions but received ${numberOfDimensions}.`, this);
}

// It looks odd to map this object to a singleton list, but that's how
Expand Down
26 changes: 14 additions & 12 deletions packages/aws-cdk-lib/aws-logs/lib/pattern.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { UnscopedValidationError } from '../../core';

// Implementation of metric patterns

/**
Expand Down Expand Up @@ -180,7 +182,7 @@ export class FilterPattern {
* A JSON log pattern that matches if all given JSON log patterns match
*/
public static all(...patterns: JsonPattern[]): JsonPattern {
if (patterns.length === 0) { throw new Error('Must supply at least one pattern, or use allEvents() to match all events.'); }
if (patterns.length === 0) { throw new UnscopedValidationError('Must supply at least one pattern, or use allEvents() to match all events.'); }
if (patterns.length === 1) { return patterns[0]; }
return new JSONAggregatePattern('&&', patterns);
}
Expand All @@ -189,7 +191,7 @@ export class FilterPattern {
* A JSON log pattern that matches if any of the given JSON log patterns match
*/
public static any(...patterns: JsonPattern[]): JsonPattern {
if (patterns.length === 0) { throw new Error('Must supply at least one pattern'); }
if (patterns.length === 0) { throw new UnscopedValidationError('Must supply at least one pattern'); }
if (patterns.length === 1) { return patterns[0]; }
return new JSONAggregatePattern('||', patterns);
}
Expand Down Expand Up @@ -282,7 +284,7 @@ class JSONPostfixPattern extends JsonPattern {
class JSONAggregatePattern extends JsonPattern {
public constructor(operator: string, patterns: JsonPattern[]) {
if (operator !== '&&' && operator !== '||') {
throw new Error('Operator must be one of && or ||');
throw new UnscopedValidationError('Operator must be one of && or ||');
}

const clauses = patterns.map(p => '(' + p.jsonPatternString + ')');
Expand Down Expand Up @@ -313,12 +315,12 @@ export class SpaceDelimitedTextPattern implements IFilterPattern {
// going through the factory
for (const column of columns) {
if (!validColumnName(column)) {
throw new Error(`Invalid column name: ${column}`);
throw new UnscopedValidationError(`Invalid column name: ${column}`);
}
}

if (sum(columns.map(c => c === COL_ELLIPSIS ? 1 : 0)) > 1) {
throw new Error("Can use at most one '...' column");
throw new UnscopedValidationError("Can use at most one '...' column");
}

return new SpaceDelimitedTextPattern(columns, {});
Expand All @@ -335,10 +337,10 @@ export class SpaceDelimitedTextPattern implements IFilterPattern {
*/
public whereString(columnName: string, comparison: string, value: string): SpaceDelimitedTextPattern {
if (columnName === COL_ELLIPSIS) {
throw new Error("Can't use '...' in a restriction");
throw new UnscopedValidationError("Can't use '...' in a restriction");
}
if (this.columns.indexOf(columnName) === -1) {
throw new Error(`Column in restrictions that is not in columns: ${columnName}`);
throw new UnscopedValidationError(`Column in restrictions that is not in columns: ${columnName}`);
}

comparison = validateStringOperator(comparison);
Expand All @@ -354,10 +356,10 @@ export class SpaceDelimitedTextPattern implements IFilterPattern {
*/
public whereNumber(columnName: string, comparison: string, value: number): SpaceDelimitedTextPattern {
if (columnName === COL_ELLIPSIS) {
throw new Error("Can't use '...' in a restriction");
throw new UnscopedValidationError("Can't use '...' in a restriction");
}
if (this.columns.indexOf(columnName) === -1) {
throw new Error(`Column in restrictions that is not in columns: ${columnName}`);
throw new UnscopedValidationError(`Column in restrictions that is not in columns: ${columnName}`);
}

comparison = validateNumericalOperator(comparison);
Expand Down Expand Up @@ -445,7 +447,7 @@ function validateStringOperator(operator: string) {
if (operator === '==') { operator = '='; }

if (operator !== '=' && operator !== '!=') {
throw new Error(`Invalid comparison operator ('${operator}'), must be either '=' or '!='`);
throw new UnscopedValidationError(`Invalid comparison operator ('${operator}'), must be either '=' or '!='`);
}

return operator;
Expand All @@ -463,7 +465,7 @@ function validateNumericalOperator(operator: string) {
if (operator === '==') { operator = '='; }

if (VALID_OPERATORS.indexOf(operator) === -1) {
throw new Error(`Invalid comparison operator ('${operator}'), must be one of ${VALID_OPERATORS.join(', ')}`);
throw new UnscopedValidationError(`Invalid comparison operator ('${operator}'), must be one of ${VALID_OPERATORS.join(', ')}`);
}

return operator;
Expand All @@ -478,7 +480,7 @@ function renderRestriction(column: string, restriction: ColumnRestriction) {
} else if (restriction.stringValue) {
return `${column} ${restriction.comparison} ${quoteTerm(restriction.stringValue)}`;
} else {
throw new Error('Invalid restriction');
throw new UnscopedValidationError('Invalid restriction');
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-logs/lib/subscription-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ILogGroup, SubscriptionFilterOptions } from './log-group';
import { CfnSubscriptionFilter } from './logs.generated';
import * as iam from '../../aws-iam';
import { KinesisDestination } from '../../aws-logs-destinations';
import { Resource, Token } from '../../core';
import { Resource, Token, ValidationError } from '../../core';
import { addConstructMetadata } from '../../core/lib/metadata-resource';

/**
Expand Down Expand Up @@ -67,7 +67,7 @@ export class SubscriptionFilter extends Resource {
!Token.isUnresolved(props.destination) &&
!(props.destination instanceof KinesisDestination)
) {
throw new Error('distribution property can only be used with KinesisDestination.');
throw new ValidationError('distribution property can only be used with KinesisDestination.', this);
}

const destProps = props.destination.bind(this, props.logGroup);
Expand Down
Loading