@cowwoc/requirements
Version:
A fluent API for enforcing design contracts with automatic message generation.
59 lines • 2.17 kB
JavaScript
/*
* Copyright (c) 2019 Gili Tzabari
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
*/
import { requireThatStringIsNotEmpty, internalValueToString, Configuration, assertThatInstanceOf, assertThatType, isErrorBuilder, Type } from "../internal.mjs";
class ValidationFailureImpl {
message;
errorBuilder;
error;
errorTransformer;
transformedError = null;
/**
* @param configuration - the validator's configuration
* @param message - the failure message
* @param errorBuilder - returns the error associated with the failure message
* @throws AssertionError if:
* <ul>
* <li>Any of the arguments are `undefined` or `null`.</li>
* <li>The error message contains leading or trailing whitespace, or is empty.</li>
* </ul>
*/
constructor(configuration, message, errorBuilder) {
assertThatInstanceOf(configuration, "configuration", Configuration);
requireThatStringIsNotEmpty(message, "message");
assertThatType(errorBuilder, "errorBuilder", Type.namedClass("ErrorBuilder", () => isErrorBuilder(errorBuilder)));
this.message = message;
if (configuration.recordStacktrace()) {
this.errorBuilder = errorBuilder;
this.error = null;
}
else {
this.errorBuilder = null;
this.error = errorBuilder(message);
}
this.errorTransformer = configuration.errorTransformer();
}
getMessage() {
return this.message;
}
getType() {
return this.getTransformedError().name;
}
getError() {
return this.getTransformedError();
}
getTransformedError() {
if (this.transformedError === null) {
if (this.error === null)
this.error = this.errorBuilder(this.message);
this.transformedError = this.errorTransformer(this.error);
}
return this.transformedError;
}
toString() {
return `error: ${internalValueToString(this.error)}`;
}
}
export { ValidationFailureImpl };
//# sourceMappingURL=ValidationFailureImpl.mjs.map