UNPKG

5.86 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __exportStar = (this && this.__exportStar) || function(m, exports) {
10 for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13exports.normalizeToDiagnostic = exports.normalizeToCompilerError = exports.invariant = exports.generateCompilerError = exports.generateCompilerDiagnostic = exports.generateErrorMessage = exports.CompilerError = void 0;
14/*
15 * Copyright (c) 2018, salesforce.com, inc.
16 * All rights reserved.
17 * SPDX-License-Identifier: MIT
18 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
19 */
20const utils_1 = require("../shared/utils");
21const utils_2 = require("./utils");
22var utils_3 = require("./utils");
23Object.defineProperty(exports, "CompilerError", { enumerable: true, get: function () { return utils_3.CompilerError; } });
24__exportStar(require("./error-info"), exports);
25function generateErrorMessage(errorInfo, args) {
26 const message = Array.isArray(args)
27 ? utils_1.templateString(errorInfo.message, args)
28 : errorInfo.message;
29 if (errorInfo.url && errorInfo.url !== '') {
30 // TODO [#1289]: Add url info into message
31 }
32 return `LWC${errorInfo.code}: ${message}`;
33}
34exports.generateErrorMessage = generateErrorMessage;
35/**
36 * Generates a compiler diagnostic. This function is used to look up the specified errorInfo
37 * and generate a friendly and consistent diagnostic object. Diagnostic contains
38 * info about the error's code and its origin (filename, line, column) when applicable.
39 *
40 * @param {LWCErrorInfo} errorInfo The object holding the error metadata.
41 * @param {ErrorConfig} config A config object providing any message arguments and origin info needed to create the error.
42 * @return {CompilerDiagnostic}
43 */
44function generateCompilerDiagnostic(errorInfo, config) {
45 const message = generateErrorMessage(errorInfo, config && config.messageArgs);
46 const diagnostic = {
47 code: errorInfo.code,
48 message,
49 level: errorInfo.level,
50 };
51 if (config && config.origin) {
52 diagnostic.filename = utils_2.getFilename(config.origin);
53 diagnostic.location = utils_2.getLocation(config.origin);
54 }
55 return diagnostic;
56}
57exports.generateCompilerDiagnostic = generateCompilerDiagnostic;
58/**
59 * Generates a compiler error. This function is used to look up the specified errorInfo
60 * and generate a friendly and consistent error object. Error object contains info about
61 * the error's code and its origin (filename, line, column) when applicable.
62 *
63 * @param {LWCErrorInfo} errorInfo The object holding the error metadata.
64 * @param {ErrorConfig} config A config object providing any message arguments and origin info needed to create the error.
65 * @return {CompilerError}
66 */
67function generateCompilerError(errorInfo, config) {
68 const message = generateErrorMessage(errorInfo, config && config.messageArgs);
69 const error = new utils_2.CompilerError(errorInfo.code, message);
70 if (config) {
71 error.filename = utils_2.getFilename(config.origin);
72 error.location = utils_2.getLocation(config.origin);
73 }
74 return error;
75}
76exports.generateCompilerError = generateCompilerError;
77function invariant(condition, errorInfo, args) {
78 if (!condition) {
79 throw generateCompilerError(errorInfo, {
80 messageArgs: args,
81 });
82 }
83}
84exports.invariant = invariant;
85/**
86 * Normalizes a received error into a CompilerError. Adds any provided additional origin info.
87 * @param errorInfo
88 * @param error
89 * @param origin
90 *
91 * @return {CompilerError}
92 */
93function normalizeToCompilerError(errorInfo, error, origin) {
94 if (error instanceof utils_2.CompilerError) {
95 if (origin) {
96 error.filename = utils_2.getFilename(origin);
97 error.location = utils_2.getLocation(origin);
98 }
99 return error;
100 }
101 const { code, message, filename, location } = convertErrorToDiagnostic(error, errorInfo, origin);
102 const compilerError = new utils_2.CompilerError(code, `${error.name}: ${message}`, filename, location);
103 compilerError.stack = error.stack;
104 return compilerError;
105}
106exports.normalizeToCompilerError = normalizeToCompilerError;
107/**
108 * Normalizes a received error into a CompilerDiagnostic. Adds any provided additional origin info.
109 * @param error
110 * @param origin
111 *
112 * @return {CompilerDiagnostic}
113 */
114function normalizeToDiagnostic(errorInfo, error, origin) {
115 if (error instanceof utils_2.CompilerError) {
116 const diagnostic = error.toDiagnostic();
117 if (origin) {
118 diagnostic.filename = utils_2.getFilename(origin);
119 diagnostic.location = utils_2.getLocation(origin);
120 }
121 return diagnostic;
122 }
123 return convertErrorToDiagnostic(error, errorInfo, origin);
124}
125exports.normalizeToDiagnostic = normalizeToDiagnostic;
126function convertErrorToDiagnostic(error, fallbackErrorInfo, origin) {
127 const code = utils_2.getCodeFromError(error) || fallbackErrorInfo.code;
128 const message = error.lwcCode
129 ? error.message
130 : generateErrorMessage(fallbackErrorInfo, [error.message]);
131 const level = error.level || fallbackErrorInfo.level;
132 const filename = utils_2.getFilename(origin, error);
133 const location = utils_2.getLocation(origin, error);
134 // TODO [#1289]: Preserve stack information
135 return { code, message, level, filename, location };
136}
137//# sourceMappingURL=errors.js.map
\No newline at end of file