Debian Codegraph @

closure-library/closure/goog/debug/errorcontext.js

raw
1 // Copyright 2017 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Provides methods dealing with context on error objects.
17 */
18
19 goog.provide('goog.debug.errorcontext');
20
21
22 /**
23 * Adds key-value context to the error.
24 * @param {!Error} err The error to add context to.
25 * @param {string} contextKey Key for the context to be added.
26 * @param {string} contextValue Value for the context to be added.
27 */
28 goog.debug.errorcontext.addErrorContext = function(
29 err, contextKey, contextValue) {
30 if (!err[goog.debug.errorcontext.CONTEXT_KEY_]) {
31 err[goog.debug.errorcontext.CONTEXT_KEY_] = {};
32 }
33 err[goog.debug.errorcontext.CONTEXT_KEY_][contextKey] = contextValue;
34 };
35
36
37 /**
38 * @param {!Error} err The error to get context from.
39 * @return {!Object<string, string>} The context of the provided error.
40 */
41 goog.debug.errorcontext.getErrorContext = function(err) {
42 return err[goog.debug.errorcontext.CONTEXT_KEY_] || {};
43 };
44
45
46 // TODO(user): convert this to a Symbol once goog.debug.ErrorReporter is
47 // able to use ES6.
48 /** @private @const {string} */
49 goog.debug.errorcontext.CONTEXT_KEY_ = '__closure__error__context__984382';