File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ export class RedactableErrorMessage {
2+ constructor (
3+ private readonly strings : TemplateStringsArray ,
4+ private readonly values : unknown [ ] ,
5+ ) { }
6+
7+ public toString ( ) : string {
8+ return this . fullMessage ;
9+ }
10+
11+ public get fullMessage ( ) : string {
12+ return this . strings
13+ . map ( ( s , i ) => s + ( this . hasValue ( i ) ? this . getValue ( i ) : "" ) )
14+ . join ( "" ) ;
15+ }
16+
17+ public get redactedMessage ( ) : string {
18+ return this . strings
19+ . map ( ( s , i ) => s + ( this . hasValue ( i ) ? "[REDACTED]" : "" ) )
20+ . join ( "" ) ;
21+ }
22+
23+ private getValue ( index : number ) : unknown {
24+ return this . values [ index ] ;
25+ }
26+
27+ private hasValue ( index : number ) : boolean {
28+ return index < this . values . length ;
29+ }
30+ }
31+
32+ export function errorMessage (
33+ strings : TemplateStringsArray ,
34+ ...values : unknown [ ]
35+ ) : RedactableErrorMessage {
36+ return new RedactableErrorMessage ( strings , values ) ;
37+ }
Original file line number Diff line number Diff line change 1+ import {
2+ errorMessage ,
3+ RedactableErrorMessage ,
4+ } from "../../../src/common/errors" ;
5+
6+ describe ( "errorMessage" , ( ) => {
7+ it ( "creates a RedactableErrorMessage" , ( ) => {
8+ expect ( errorMessage `Failed to create database ${ "foo" } ` ) . toBeInstanceOf (
9+ RedactableErrorMessage ,
10+ ) ;
11+ } ) ;
12+
13+ it ( "toString() matches the given message" , ( ) => {
14+ expect ( errorMessage `Failed to create database ${ "foo" } ` . toString ( ) ) . toEqual (
15+ "Failed to create database foo" ,
16+ ) ;
17+ } ) ;
18+
19+ it ( "fullMessage matches the given message" , ( ) => {
20+ expect (
21+ errorMessage `Failed to create database ${ "foo" } ` . fullMessage ,
22+ ) . toEqual ( "Failed to create database foo" ) ;
23+ } ) ;
24+
25+ it ( "redactedMessage redacts the given message" , ( ) => {
26+ expect (
27+ errorMessage `Failed to create database ${ "foo" } ` . redactedMessage ,
28+ ) . toEqual ( "Failed to create database [REDACTED]" ) ;
29+ } ) ;
30+ } ) ;
You can’t perform that action at this time.
0 commit comments