-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
error.js
76 lines (67 loc) 路 2.51 KB
/
error.js
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
import {show} from './fn';
import {ordinal, namespace, name, version} from './const';
import type from 'sanctuary-type-identifiers';
export function error(message){
return new Error(message);
}
export function typeError(message){
return new TypeError(message);
}
export function invalidArgument(it, at, expected, actual){
return typeError(
it + ' expects its ' + ordinal[at] + ' argument to ' + expected + '\n Actual: ' + show(actual)
);
}
export function invalidContext(it, actual){
return typeError(
it + ' was invoked outside the context of a Future. You might want to use'
+ ' a dispatcher instead\n Called on: ' + show(actual)
);
}
function invalidNamespace(m, x){
return (
'The Future was not created by ' + namespace + '. '
+ 'Make sure you transform other Futures to ' + namespace + ' Futures. '
+ 'Got ' + (x ? ('a Future from ' + x) : 'an unscoped Future') + '.'
+ '\n See: https://github.com/fluture-js/Fluture#casting-futures'
);
}
function invalidVersion(m, x){
return (
'The Future was created by ' + (x < version ? 'an older' : 'a newer')
+ ' version of ' + namespace + '. '
+ 'This means that one of the sources which creates Futures is outdated. '
+ 'Update this source, or transform its created Futures to be compatible.'
+ '\n See: https://github.com/fluture-js/Fluture#casting-futures'
);
}
export function invalidFuture(it, at, m, s){
var id = type.parse(type(m));
var info = id.name === name ? '\n' + (
id.namespace !== namespace ? invalidNamespace(m, id.namespace)
: id.version !== version ? invalidVersion(m, id.version)
: 'Nothing seems wrong. Contact the Fluture maintainers.') : '';
return typeError(
it + ' expects ' + (ordinal[at] ? 'its ' + ordinal[at] + ' argument to be a valid Future' : at)
+ '.' + info + '\n Actual: ' + show(m) + ' :: ' + id.name + (s || '')
);
}
function indent(s){
return ' ' + s;
}
export function someError(action, e, s){
var context = typeof s === 'string' ? '\n\n In: ' + s : '';
try{
var name = e && e.name ? String(e.name) : 'An error';
var errorMessage = (
e && e.message ? String(e.message) :
e && typeof e.toString === 'function' ? e.toString() :
String(e)
);
var message = errorMessage.trim().split('\n').map(indent).join('\n');
return error(name + ' came up while ' + action + ':\n' + message + context + '\n');
}catch(_){
return error('Something came up while ' + action + ', ' +
'but it could not be converted to string' + context + '\n');
}
}