]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/lib/utils/error-handler.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / lib / utils / error-handler.js
1
2 module.exports = errorHandler
3
4 var cbCalled = false
5   , log = require("npmlog")
6   , npm = require("../npm.js")
7   , rm = require("rimraf")
8   , itWorked = false
9   , path = require("path")
10   , wroteLogFile = false
11   , exitCode = 0
12   , rollbacks = npm.rollbacks
13   , chain = require("slide").chain
14   , writeStreamAtomic = require("fs-write-stream-atomic")
15   , nameValidator = require("validate-npm-package-name")
16
17
18 process.on("exit", function (code) {
19   // console.error("exit", code)
20   if (!npm.config || !npm.config.loaded) return
21   if (code) itWorked = false
22   if (itWorked) log.info("ok")
23   else {
24     if (!cbCalled) {
25       log.error("", "cb() never called!")
26     }
27
28     if (wroteLogFile) {
29       // just a line break
30       if (log.levels[log.level] <= log.levels.error) console.error("")
31
32       log.error("",
33                 ["Please include the following file with any support request:"
34                 ,"    " + path.resolve("npm-debug.log")
35                 ].join("\n"))
36       wroteLogFile = false
37     }
38     if (code) {
39       log.error("code", code)
40     }
41   }
42
43   var doExit = npm.config.get("_exit")
44   if (doExit) {
45     // actually exit.
46     if (exitCode === 0 && !itWorked) {
47       exitCode = 1
48     }
49     if (exitCode !== 0) process.exit(exitCode)
50   } else {
51     itWorked = false // ready for next exit
52   }
53 })
54
55 function exit (code, noLog) {
56   exitCode = exitCode || process.exitCode || code
57
58   var doExit = npm.config ? npm.config.get("_exit") : true
59   log.verbose("exit", [code, doExit])
60   if (log.level === "silent") noLog = true
61
62   if (rollbacks.length) {
63     chain(rollbacks.map(function (f) {
64       return function (cb) {
65         npm.commands.unbuild([f], true, cb)
66       }
67     }), function (er) {
68       if (er) {
69         log.error("error rolling back", er)
70         if (!code) errorHandler(er)
71         else if (noLog) rm("npm-debug.log", reallyExit.bind(null, er))
72         else writeLogFile(reallyExit.bind(this, er))
73       } else {
74         if (!noLog && code) writeLogFile(reallyExit)
75         else rm("npm-debug.log", reallyExit)
76       }
77     })
78     rollbacks.length = 0
79   }
80   else if (code && !noLog) writeLogFile(reallyExit)
81   else rm("npm-debug.log", reallyExit)
82
83   function reallyExit (er) {
84     if (er && !code) code = typeof er.errno === "number" ? er.errno : 1
85
86     // truncate once it's been written.
87     log.record.length = 0
88
89     itWorked = !code
90
91     // just emit a fake exit event.
92     // if we're really exiting, then let it exit on its own, so that
93     // in-process stuff can finish or clean up first.
94     if (!doExit) process.emit("exit", code)
95     npm.spinner.stop()
96   }
97 }
98
99
100 function errorHandler (er) {
101   // console.error("errorHandler", er)
102   if (!npm.config || !npm.config.loaded) {
103     // logging won't work unless we pretend that it's ready
104     er = er || new Error("Exit prior to config file resolving.")
105     console.error(er.stack || er.message)
106   }
107
108   if (cbCalled) {
109     er = er || new Error("Callback called more than once.")
110   }
111
112   cbCalled = true
113   if (!er) return exit(0)
114   if (typeof er === "string") {
115     log.error("", er)
116     return exit(1, true)
117   } else if (!(er instanceof Error)) {
118     log.error("weird error", er)
119     return exit(1, true)
120   }
121
122   var m = er.code || er.message.match(/^(?:Error: )?(E[A-Z]+)/)
123   if (m && !er.code) er.code = m
124
125   ; [ "type"
126     , "fstream_path"
127     , "fstream_unc_path"
128     , "fstream_type"
129     , "fstream_class"
130     , "fstream_finish_call"
131     , "fstream_linkpath"
132     , "stack"
133     , "fstream_stack"
134     , "statusCode"
135     , "pkgid"
136     ].forEach(function (k) {
137       var v = er[k]
138       if (!v) return
139       if (k === "fstream_stack") v = v.join("\n")
140       log.verbose(k, v)
141     })
142
143   log.verbose("cwd", process.cwd())
144
145   var os = require("os")
146   // log.error("System", os.type() + " " + os.release())
147   // log.error("command", process.argv.map(JSON.stringify).join(" "))
148   // log.error("node -v", process.version)
149   // log.error("npm -v", npm.version)
150   log.error("", os.type() + " " + os.release())
151   log.error("argv", process.argv.map(JSON.stringify).join(" "))
152   log.error("node", process.version)
153   log.error("npm ", "v" + npm.version)
154
155   ; [ "file"
156     , "path"
157     , "code"
158     , "errno"
159     , "syscall"
160     ].forEach(function (k) {
161       var v = er[k]
162       if (v) log.error(k, v)
163     })
164
165   // just a line break
166   if (log.levels[log.level] <= log.levels.error) console.error("")
167
168   switch (er.code) {
169   case "ECONNREFUSED":
170     log.error("", er)
171     log.error("", ["\nIf you are behind a proxy, please make sure that the"
172               ,"'proxy' config is set properly.  See: 'npm help config'"
173               ].join("\n"))
174     break
175
176   case "EACCES":
177   case "EPERM":
178     log.error("", er)
179     log.error("", ["\nPlease try running this command again as root/Administrator."
180               ].join("\n"))
181     break
182
183   case "ELIFECYCLE":
184     log.error("", er.message)
185     log.error("", ["","Failed at the "+er.pkgid+" "+er.stage+" script '"+er.script+"'."
186               ,"This is most likely a problem with the "+er.pkgname+" package,"
187               ,"not with npm itself."
188               ,"Tell the author that this fails on your system:"
189               ,"    "+er.script
190               ,'You can get information on how to open an issue for this project with:'
191               ,'    npm bugs ' + er.pkgname
192               ,'Or if that isn\'t available, you can get their info via:',
193               ,'    npm owner ls ' + er.pkgname
194               ,"There is likely additional logging output above."
195               ].join("\n"))
196     break
197
198   case "ENOGIT":
199     log.error("", er.message)
200     log.error("", ["","Failed using git."
201               ,"This is most likely not a problem with npm itself."
202               ,"Please check if you have git installed and in your PATH."
203               ].join("\n"))
204     break
205
206   case "EJSONPARSE":
207     log.error("", er.message)
208     log.error("", "File: "+er.file)
209     log.error("", ["Failed to parse package.json data."
210               ,"package.json must be actual JSON, not just JavaScript."
211               ,"","This is not a bug in npm."
212               ,"Tell the package author to fix their package.json file."
213               ].join("\n"), "JSON.parse")
214     break
215
216   // TODO(isaacs)
217   // Add a special case here for E401 and E403 explaining auth issues?
218
219   case "E404":
220     var msg = [er.message]
221     if (er.pkgid && er.pkgid !== "-") {
222       msg.push("", "'" + er.pkgid + "' is not in the npm registry.")
223
224       var valResult = nameValidator(er.pkgid)
225
226       if (valResult.validForNewPackages) {
227         msg.push("You should bug the author to publish it (or use the name yourself!)")
228       } else {
229         msg.push("Your package name is not valid, because", "")
230
231         var errorsArray = (valResult.errors || []).concat(valResult.warnings || [])
232         errorsArray.forEach(function(item, idx) {
233           msg.push(" " + (idx + 1) + ". " + item)
234         })
235       }
236
237       if (er.parent) {
238         msg.push("It was specified as a dependency of '"+er.parent+"'")
239       }
240       msg.push("\nNote that you can also install from a"
241               ,"tarball, folder, http url, or git url.")
242     }
243     // There's no need to have 404 in the message as well.
244     msg[0] = msg[0].replace(/^404\s+/, "")
245     log.error("404", msg.join("\n"))
246     break
247
248   case "EPUBLISHCONFLICT":
249     log.error("publish fail", ["Cannot publish over existing version."
250               ,"Update the 'version' field in package.json and try again."
251               ,""
252               ,"To automatically increment version numbers, see:"
253               ,"    npm help version"
254               ].join("\n"))
255     break
256
257   case "EISGIT":
258     log.error("git", [er.message
259               ,"    "+er.path
260               ,"Refusing to remove it. Update manually,"
261               ,"or move it out of the way first."
262               ].join("\n"))
263     break
264
265   case "ECYCLE":
266     log.error("cycle", [er.message
267               ,"While installing: "+er.pkgid
268               ,"Found a pathological dependency case that npm cannot solve."
269               ,"Please report this to the package author."
270               ].join("\n"))
271     break
272
273   case "EBADPLATFORM":
274     log.error("notsup", [er.message
275               ,"Not compatible with your operating system or architecture: "+er.pkgid
276               ,"Valid OS:    "+er.os.join(",")
277               ,"Valid Arch:  "+er.cpu.join(",")
278               ,"Actual OS:   "+process.platform
279               ,"Actual Arch: "+process.arch
280               ].join("\n"))
281     break
282
283   case "EEXIST":
284     log.error([er.message
285               ,"File exists: "+er.path
286               ,"Move it away, and try again."].join("\n"))
287     break
288
289   case "ENEEDAUTH":
290     log.error("need auth", [er.message
291               ,"You need to authorize this machine using `npm adduser`"
292               ].join("\n"))
293     break
294
295   case "EPEERINVALID":
296     var peerErrors = Object.keys(er.peersDepending).map(function (peer) {
297       return "Peer " + peer + " wants " + er.packageName + "@"
298         + er.peersDepending[peer]
299     })
300     log.error("peerinvalid", [er.message].concat(peerErrors).join("\n"))
301     break
302
303   case "ECONNRESET":
304   case "ENOTFOUND":
305   case "ETIMEDOUT":
306   case "EAI_FAIL":
307     log.error("network", [er.message
308               ,"This is most likely not a problem with npm itself"
309               ,"and is related to network connectivity."
310               ,"In most cases you are behind a proxy or have bad network settings."
311               ,"\nIf you are behind a proxy, please make sure that the"
312               ,"'proxy' config is set properly.  See: 'npm help config'"
313               ].join("\n"))
314     break
315
316   case "ENOPACKAGEJSON":
317     log.error("package.json", [er.message
318               ,"This is most likely not a problem with npm itself."
319               ,"npm can't find a package.json file in your current directory."
320               ].join("\n"))
321     break
322
323   case "ETARGET":
324     var msg = [er.message
325               ,"This is most likely not a problem with npm itself."
326               ,"In most cases you or one of your dependencies are requesting"
327               ,"a package version that doesn't exist."
328               ]
329       if (er.parent) {
330         msg.push("\nIt was specified as a dependency of '"+er.parent+"'\n")
331       }
332       log.error("notarget", msg.join("\n"))
333     break
334
335   case "ENOTSUP":
336     if (er.required) {
337       log.error("notsup", [er.message
338                 ,"Not compatible with your version of node/npm: "+er.pkgid
339                 ,"Required: "+JSON.stringify(er.required)
340                 ,"Actual:   "
341                 +JSON.stringify({npm:npm.version
342                                 ,node:npm.config.get("node-version")})
343                 ].join("\n"))
344       break
345     } // else passthrough
346
347   case "ENOSPC":
348     log.error("nospc", [er.message
349               ,"This is most likely not a problem with npm itself"
350               ,"and is related to insufficient space on your system."
351               ].join("\n"))
352     break
353
354   case "EROFS":
355     log.error("rofs", [er.message
356               ,"This is most likely not a problem with npm itself"
357               ,"and is related to the file system being read-only."
358               ,"\nOften virtualized file systems, or other file systems"
359               ,"that don't support symlinks, give this error."
360               ].join("\n"))
361     break
362
363   case "ENOENT":
364     log.error("enoent", [er.message
365               ,"This is most likely not a problem with npm itself"
366               ,"and is related to npm not being able to find a file."
367               ,er.file?"\nCheck if the file '"+er.file+"' is present.":""
368               ].join("\n"))
369     break
370
371   case "EISDIR":
372     log.error("eisdir", [er.message
373               ,"This is most likely not a problem with npm itself"
374               ,"and is related to npm not being able to find a package.json in"
375               ,"a package you are trying to install."
376               ].join("\n"))
377     break
378
379   default:
380     log.error("", er.message || er)
381     log.error("", ["", "If you need help, you may report this error at:"
382                   ,"    <https://github.com/npm/npm/issues>"
383                   ].join("\n"))
384     break
385   }
386
387   exit(typeof er.errno === "number" ? er.errno : 1)
388 }
389
390 var writingLogFile = false
391 function writeLogFile (cb) {
392   if (writingLogFile) return cb()
393   writingLogFile = true
394   wroteLogFile = true
395
396   var fstr = writeStreamAtomic("npm-debug.log")
397     , os = require("os")
398     , out = ""
399
400   log.record.forEach(function (m) {
401     var pref = [m.id, m.level]
402     if (m.prefix) pref.push(m.prefix)
403     pref = pref.join(" ")
404
405     m.message.trim().split(/\r?\n/).map(function (line) {
406       return (pref + " " + line).trim()
407     }).forEach(function (line) {
408       out += line + os.EOL
409     })
410   })
411
412   fstr.end(out)
413   fstr.on("close", cb)
414 }