UNPKG

6.21 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 */
18function AppCError(message, id) {
19 Error.call(this);
20 Error.captureStackTrace(this, AppCError);
21 this.id = id;
22 this.name = 'AppCError';
23 this.message = message;
24 debug('creating AppCError %s, %s',message,id);
25}
26util.inherits(AppCError, Error);
27
28/**
29 * print stack trace and exit with failure
30 */
31AppCError.prototype.failWithStackTrace = function() {
32 u.fail(this.message+chalk.grey(" ["+this.id+"]"));
33};
34
35const ERRORS = {
36 'com.appcelerator.install.binary.missing': {
37 message: "Cannot find expected binary at %s. This likely means the install package at %s is invalid for version %s",
38 argcount: 3,
39 },
40 'com.appcelerator.install.binary.error': {
41 message: "Unexpected error running %s. %s",
42 argcount: 2,
43 },
44 'com.appcelerator.install.download.server.response.error': {
45 message: 'Server responded with unexpected error: %s. Please re-try your install again. If you continue to have this problem, please contact Appcelerator Support at support@appcelerator.com.',
46 argcount: 1
47 },
48 'com.appcelerator.install.download.server.stream.error': {
49 message: 'Unexpected error received during download. %s. Please re-try your install again. If you continue to have this problem, please contact Appcelerator Support at support@appcelerator.com.',
50 argcount: 1
51 },
52 'com.appcelerator.install.download.version.specified.incorrect': {
53 message: 'The version specified %s was not found',
54 argcount: 1
55 },
56 'com.appcelerator.install.download.invalid.content.length': {
57 message: 'Received invalid response from download server (content-length was not set). Please re-try your install again.'
58 },
59 'com.appcelerator.install.download.failed.retries.max': {
60 message: 'Download failed after %d failed re-attempts. 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.',
61 argcount: 1
62 },
63 'com.appcelerator.install.download.failed.checksum': {
64 message: 'Invalid file download checksum. 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.',
65 argcount: 2
66 },
67 'com.appcelerator.install.download.server.unavailable': {
68 message: 'Download server is not currently available. 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.'
69 },
70 'com.appcelerator.install.download.server.response.unexpected': {
71 message: 'Unexpected response returned from server (%d). Please re-try your install again.',
72 argcount:1
73 },
74 'com.appcelerator.install.installer.sudo': {
75 message: 'Ooops! You cannot run using sudo. Please re-run using the %d account.',
76 argcount: 1
77 },
78 'com.appcelerator.install.installer.user.root': {
79 message: 'Ooops! You cannot run as root. Please re-run using a user account.',
80 },
81 'com.appcelerator.install.installer.user.sudo.user': {
82 message: 'Ooops! You cannot run using sudo as another user. Please re-run using the %s account.',
83 argcount: 1
84 },
85 'com.appcelerator.install.installer.missing.homedir': {
86 message: 'Ooops! Your home directory (%s) cannot be found. Please make sure that the environment variable %s is set correctly to the correct directory and that it is writable.',
87 argcount: 2
88 },
89 'com.appcelerator.install.installer.extraction.failed': {
90 message: 'Download file extraction failed. Please re-run again.'
91 },
92 'com.appcelerator.install.preflight.directory.unwritable': {
93 message: 'Ooops! Your %s directory (%s) is not writable.\n%s',
94 argcount: 3
95 },
96 'com.appcelerator.install.preflight.directory.ownership': {
97 message: 'Ooops! Your %s directory (%s) is not owned by %s.\n%s',
98 argcount: 4
99 },
100 'com.appcelerator.install.preflight.missing.xcode.clitools': {
101 message: 'Xcode command line developers tools are required. Choose an option in the dialog to download the command line developer tools. Once you have completed the installation, please re-run this command.'
102 },
103 'com.appcelerator.install.use.download.error': {
104 message: 'Unexpected error received fetching latest version details. %s Please re-try your request again. If you continue to have this problem, please contact Appcelerator Support at support@appcelerator.com.',
105 argcount: 1
106 }
107};
108
109/**
110 * construct the proper error message
111 */
112function createErrorMessage(errorcode) {
113 if (errorcode in ERRORS) {
114 var args = Array.prototype.slice.call(arguments,1);
115 var entry = ERRORS[errorcode];
116 if (entry.argcount && entry.argcount!==args.length) {
117 u.fail("Internal failure. Unexpected usage of internal command. Please report error code: "+errorcode+"(invalid args) to Appcelerator Support with the following stack trace:"+new Error().stack);
118 }
119 return args.length ? util.format.apply(util.format,[entry.message].concat(args)) : entry.message;
120 }
121 else {
122 u.fail("Internal failure. Unexpected usage of internal command. Please report error code: "+errorcode+"(invalid error code) to Appcelerator Support with the following stack trace:"+new Error().stack);
123 }
124}
125
126/**
127 * fail with an error (console.error + exitcode 1)
128 */
129exports.failWithError = function(errorcode) {
130 var message = createErrorMessage.apply(null,arguments);
131 if (message) {
132 u.fail(message+chalk.grey(" ["+errorcode+"]"));
133 }
134};
135
136/**
137 * create an AppCError error class and return an instance
138 */
139exports.createError = function(errorcode) {
140 var message = createErrorMessage.apply(null,arguments);
141 if (message) {
142 return new AppCError(message,errorcode);
143 }
144};
145
146exports.ERRORS = ERRORS;
\No newline at end of file