1 |
|
2 |
|
3 |
|
4 | (function(factory) {
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | factory(function() {
|
12 |
|
13 | var toString = Object.prototype.toString;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | return function() {
|
19 | var args = new Array(arguments.length);
|
20 | for (var i = 0; i < args.length; ++i) {
|
21 | args[i] = arguments[i];
|
22 | }
|
23 | var name = getName(args);
|
24 | var target = getTarget(args);
|
25 | var properties = getProps(args);
|
26 | function ErrorCtor(message, obj) {
|
27 | attachProps(this, properties);
|
28 | attachProps(this, obj);
|
29 | this.message = (message || this.message);
|
30 | if (message instanceof Error) {
|
31 | this.message = message.message;
|
32 | this.stack = message.stack;
|
33 | } else if (Error.captureStackTrace) {
|
34 | Error.captureStackTrace(this, this.constructor);
|
35 | }
|
36 | }
|
37 | function Err() { this.constructor = ErrorCtor; }
|
38 | Err.prototype = target['prototype'];
|
39 | ErrorCtor.prototype = new Err();
|
40 | ErrorCtor.prototype.name = ('' + name) || 'CustomError';
|
41 | return ErrorCtor;
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 | function getName(args) {
|
47 | if (args.length === 0) return '';
|
48 | return isError(args[0]) ? (args[1] || '') : args[0];
|
49 | }
|
50 | function getTarget(args) {
|
51 | if (args.length === 0) return Error;
|
52 | return isError(args[0]) ? args[0] : Error;
|
53 | }
|
54 | function getProps(args) {
|
55 | if (args.length === 0) return null;
|
56 | return isError(args[0]) ? args[2] : args[1];
|
57 | }
|
58 | function inheritedKeys(obj) {
|
59 | var ret = [];
|
60 | for (var key in obj) {
|
61 | ret.push(key);
|
62 | }
|
63 | return ret;
|
64 | }
|
65 |
|
66 |
|
67 | function isError(obj) {
|
68 | return (typeof obj === "function");
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 | function isObject(obj) {
|
74 | return (obj && typeof obj === "object" && toString.call(obj) === "[object Object]");
|
75 | }
|
76 |
|
77 |
|
78 | function attachProps(context, target) {
|
79 | if (isObject(target)) {
|
80 | var keys = inheritedKeys(target);
|
81 | for (var i = 0, l = keys.length; i < l; ++i) {
|
82 | context[keys[i]] = clone(target[keys[i]]);
|
83 | }
|
84 | }
|
85 | }
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | function clone(target) {
|
91 | if (target == null || typeof target !== "object") return target;
|
92 | var cloned = target.constructor ? target.constructor() : Object.create(null);
|
93 | for (var attr in target) {
|
94 | if (target.hasOwnProperty(attr)) {
|
95 | cloned[attr] = target[attr];
|
96 | }
|
97 | }
|
98 | return cloned;
|
99 | }
|
100 |
|
101 | });
|
102 |
|
103 |
|
104 | })(function(createErrorLib) {
|
105 | if (typeof define === "function" && define.amd) {
|
106 | define(createErrorLib);
|
107 | } else if (typeof exports === 'object') {
|
108 | module.exports = createErrorLib();
|
109 | } else {
|
110 | var root = this;
|
111 | var lastcreateError = root.createError;
|
112 | var createError = root.createError = createErrorLib();
|
113 | createError.noConflict = function() {
|
114 | root.createError = lastcreateError;
|
115 | return createError;
|
116 | };
|
117 | }
|
118 | });
|