/
configurationError.ts
80 lines (76 loc) · 2.99 KB
/
configurationError.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import {ResolutionMethod} from '../types/publicTypes';
/** Alias for Resolution error handler function
* @internal
*/
type ConfigurationErrorHandler = (error: ConfigurationErrorOptions) => string;
/** Explains Resolution Error options */
type ConfigurationErrorOptions = {
method?: ResolutionMethod;
dependency?: string;
config?: string;
field?: string;
version?: string;
};
export enum ConfigurationErrorCode {
IncorrectProvider = 'IncorrectProvider',
UnsupportedNetwork = 'UnsupportedNetwork',
UnspecifiedUrl = 'UnspecifiedUrl',
NetworkConfigMissing = 'NetworkConfigMissing',
CustomNetworkConfigMissing = 'CustomNetworkConfigMissing',
InvalidConfigurationField = 'InvalidProxyReader',
}
/**
* @internal
* Internal Mapping object from ConfigurationErrorCode to a ConfigurationErrorHandler
*/
const HandlersByCode = {
[ConfigurationErrorCode.IncorrectProvider]: () =>
"Provider doesn't implement sendAsync or send method",
[ConfigurationErrorCode.UnsupportedNetwork]: (params: {
method: ResolutionMethod;
}) =>
`Unsupported network in Resolution ${params.method || ''} configuration`,
[ConfigurationErrorCode.UnspecifiedUrl]: (params: {
method: ResolutionMethod;
}) => `Unspecified url in Resolution ${params.method} configuration`,
[ConfigurationErrorCode.NetworkConfigMissing]: (params: {
method: ResolutionMethod;
config: string;
}) =>
`Missing configuration in Resolution ${params.method}. Please specify ${params.config}`,
[ConfigurationErrorCode.CustomNetworkConfigMissing]: (params: {
method: ResolutionMethod;
config: string;
}) =>
`Missing configuration in Resolution ${params.method}. Please specify ${params.config} when using a custom network`,
[ConfigurationErrorCode.InvalidConfigurationField]: (params: {
method: ResolutionMethod;
field: string;
}) => `Invalid '${params.field}' in Resolution ${params.method}`,
};
/**
* Configuration Error class is designed to control every error being thrown by wrong configurations for objects
* @param code - Error Code
* - IncorrectProvider - When provider doesn't have implemented send or sendAsync methods
* - UnsupportedNetwork - When network is not specified or not supported
* - UnspecifiedUrl - When url is not specified for custom naming service configurations
* - CustomNetworkConfigMissing - When configuration is missing for custom network configurations
* @param method - optional param to specify which namingService errored out
*/
export class ConfigurationError extends Error {
readonly code: ConfigurationErrorCode;
readonly method?: string;
constructor(
code: ConfigurationErrorCode,
options: ConfigurationErrorOptions = {},
) {
const configurationErrorHandler: ConfigurationErrorHandler =
HandlersByCode[code];
super(configurationErrorHandler(options));
this.code = code;
this.method = options.method;
this.name = 'ConfigurationError';
Object.setPrototypeOf(this, ConfigurationError.prototype);
}
}
export default ConfigurationError;