UNPKG

6.67 kBJavaScriptView Raw
1/**
2 * This code is closed source and Confidential and Proprietary to
3 * Appcelerator, Inc. All Rights Reserved. This code MUST not be
4 * modified, copied or otherwise redistributed without express
5 * written permission of Appcelerator. This file is licensed as
6 * part of the Appcelerator Platform and governed under the terms
7 * of the Appcelerator license agreement.
8 */
9/* jshint esnext: true */
10var util = require('util'),
11 u = require('./util'),
12 chalk = require('chalk'),
13 debug = require('debug')('appc:error');
14
15/**
16 * create a custom error so we can get proper error code
17 * @param {string} message - The error message.
18 * @param {string} id - The ID for the error.
19 */
20function AppCError(message, id) {
21 Error.call(this);
22 Error.captureStackTrace(this, AppCError);
23 this.id = id;
24 this.name = 'AppCError';
25 this.message = message;
26 debug('creating AppCError %s, %s', message, id);
27}
28util.inherits(AppCError, Error);
29
30/**
31 * print stack trace and exit with failure
32 */
33AppCError.prototype.failWithStackTrace = function () {
34 u.fail(this.message + chalk.grey(' [' + this.id + ']'));
35};
36
37const ERRORS = {
38 'com.appcelerator.install.binary.missing': {
39 message: 'Cannot find expected binary at %s. '
40 + 'This likely means the install package at %s is invalid for version %s',
41 argcount: 3
42 },
43 'com.appcelerator.install.binary.error': {
44 message: 'Unexpected error running %s. %s',
45 argcount: 2
46 },
47 'com.appcelerator.install.download.server.response.error': {
48 message: 'Server responded with unexpected error: %s. Please re-try your install again. '
49 + 'If you continue to have this problem, please contact Appcelerator Support at support@appcelerator.com.',
50 argcount: 1
51 },
52 'com.appcelerator.install.download.server.stream.error': {
53 message: 'Unexpected error received during download. %s. Please re-try your install again. '
54 + 'If you continue to have this problem, please contact Appcelerator Support at support@appcelerator.com.',
55 argcount: 1
56 },
57 'com.appcelerator.install.download.version.specified.incorrect': {
58 message: 'The version specified %s was not found',
59 argcount: 1
60 },
61 'com.appcelerator.install.download.invalid.content.length': {
62 message: 'Received invalid response from download server (content-length was not set). '
63 + 'Please re-try your install again.'
64 },
65 'com.appcelerator.install.download.failed.retries.max': {
66 message: 'Download failed after %d failed re-attempts. '
67 + 'Please re-try your install again in a few moments. If you continue to have this problem, please contact Appcelerator Support at support@appcelerator.com.',
68 argcount: 1
69 },
70 'com.appcelerator.install.download.failed.checksum': {
71 message: 'Invalid file download checksum. '
72 + 'This could be a result of the file being modified in transit or it could be because the download was interrupted or had an error. Expected: %s, was: %s. Please re-try this install again.',
73 argcount: 2
74 },
75 'com.appcelerator.install.download.server.unavailable': {
76 message: 'Download server is not currently available. '
77 + 'Please re-try your install again in a few moments. '
78 + 'If you continue to have this problem, please contact Appcelerator Support at support@appcelerator.com.'
79 },
80 'com.appcelerator.install.download.server.response.unexpected': {
81 message: 'Unexpected response returned from server (%d). '
82 + 'Please re-try your install again.',
83 argcount: 1
84 },
85 'com.appcelerator.install.installer.sudo': {
86 message: 'Ooops! You cannot run using sudo. Please re-run using the %d account.',
87 argcount: 1
88 },
89 'com.appcelerator.install.installer.user.root': {
90 message: 'Ooops! You cannot run as root. Please re-run using a user account.'
91 },
92 'com.appcelerator.install.installer.user.sudo.user': {
93 message: 'Ooops! You cannot run using sudo as another user. '
94 + 'Please re-run using the %s account.',
95 argcount: 1
96 },
97 'com.appcelerator.install.installer.missing.homedir': {
98 message: 'Ooops! Your home directory (%s) cannot be found. '
99 + 'Please make sure that the environment variable %s is set correctly to the correct directory and that it is writable.',
100 argcount: 2
101 },
102 'com.appcelerator.install.installer.extraction.failed': {
103 message: 'Download file extraction failed. Please re-run again.'
104 },
105 'com.appcelerator.install.preflight.directory.unwritable': {
106 message: 'Ooops! Your %s directory (%s) is not writable.\n%s',
107 argcount: 3
108 },
109 'com.appcelerator.install.preflight.directory.ownership': {
110 message: 'Ooops! Your %s directory (%s) is not owned by %s.\n%s',
111 argcount: 4
112 },
113 'com.appcelerator.install.preflight.missing.xcode.clitools': {
114 message: 'Xcode command line developers tools are required. '
115 + 'Choose an option in the dialog to download the command line developer tools. '
116 + 'Once you have completed the installation, please re-run this command.'
117 },
118 'com.appcelerator.install.use.download.error': {
119 message: 'Unexpected error received fetching latest version details. %s Please re-try your request again. '
120 + 'If you continue to have this problem, please contact Appcelerator Support at support@appcelerator.com.',
121 argcount: 1
122 }
123};
124
125/**
126 * construct the proper error message
127 * @param {string} errorcode - The errorcode for the error.
128 * @returns {string}
129 */
130function createErrorMessage(errorcode) {
131 if (errorcode in ERRORS) {
132 var args = Array.prototype.slice.call(arguments, 1);
133 var entry = ERRORS[errorcode];
134 if (entry.argcount && entry.argcount !== args.length) {
135 u.fail('Internal failure. Unexpected usage of internal command. Please report error code: ' + errorcode + '(invalid args) '
136 + 'to Appcelerator Support with the following stack trace:' + new Error().stack);
137 }
138 return args.length ? util.format.apply(util.format, [ entry.message ].concat(args)) : entry.message;
139 } else {
140 u.fail('Internal failure. Unexpected usage of internal command. Please report error code: ' + errorcode + '(invalid error code) '
141 + 'to Appcelerator Support with the following stack trace:' + new Error().stack);
142 }
143}
144
145/**
146 * fail with an error (console.error + exitcode 1)
147 * @param {string} errorcode - The errorcode for the error.
148 */
149exports.failWithError = function (errorcode) {
150 var message = createErrorMessage.apply(null, arguments);
151 if (message) {
152 u.fail(message + chalk.grey(' [' + errorcode + ']'));
153 }
154};
155
156/**
157 * create an AppCError error class and return an instance
158 * @param {string} errorcode - The errorcode for the error.
159 * @returns {AppCError}
160 */
161exports.createError = function (errorcode) {
162 var message = createErrorMessage.apply(null, arguments);
163 if (message) {
164 return new AppCError(message, errorcode);
165 }
166};
167
168exports.ERRORS = ERRORS;