1//  Copyright (c) 2015-2016 Couchbase, Inc.
2//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
3//  except in compliance with the License. You may obtain a copy of the License at
4//    http://www.apache.org/licenses/LICENSE-2.0
5//  Unless required by applicable law or agreed to in writing, software distributed under the
6//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
7//  either express or implied. See the License for the specific language governing permissions
8//  and limitations under the License.
9
10package command
11
12import (
13	"fmt"
14	"io"
15
16	"github.com/couchbase/query/errors"
17)
18
19/* The following variables are used to display the error
20   messages in red text and then reset the terminal prompt
21   color.
22*/
23var reset = "\x1b[0m"
24var fgRed = "\x1b[31m"
25
26// Methods that get and set display variables
27
28func SetDispVal(newreset, newfgRed string) {
29	reset = newreset
30	fgRed = newfgRed
31}
32
33func Getreset() string {
34	return reset
35}
36
37func GetfgRed() string {
38	return fgRed
39}
40
41/* The handleError method creates the error using the methods
42   defined in the n1ql errors package. This is where all the
43   shell errors are handled.
44*/
45
46func HandleError(err int, msg string) errors.Error {
47
48	switch err {
49
50	//Connection errors
51	case errors.CONNECTION_REFUSED:
52		return errors.NewShellErrorCannotConnect(msg)
53	case errors.UNSUPPORTED_PROTOCOL:
54		return errors.NewShellErrorUnsupportedProtocol(SERVICE_URL)
55	case errors.NO_SUCH_HOST:
56		return errors.NewShellErrorNoSuchHost(SERVICE_URL)
57	case errors.NO_HOST_IN_URL:
58		return errors.NewShellErrorNoHostInRequestUrl(SERVICE_URL)
59	case errors.UNKNOWN_PORT_TCP:
60		return errors.NewShellErrorUnknownPorttcp(SERVICE_URL)
61	case errors.NO_ROUTE_TO_HOST:
62		return errors.NewShellErrorNoRouteToHost(SERVICE_URL)
63	case errors.UNREACHABLE_NETWORK:
64		return errors.NewShellErrorUnreachableNetwork("")
65	case errors.NO_CONNECTION:
66		return errors.NewShellErrorNoConnection("")
67	case errors.DRIVER_OPEN:
68		return errors.NewShellErrorDriverOpen(msg)
69	case errors.INVALID_URL:
70		return errors.NewShellErrorInvalidURL(msg)
71
72	//Read/Write/Update file errors
73	case errors.READ_FILE:
74		return errors.NewShellErrorReadFile(msg)
75	case errors.WRITE_FILE:
76		return errors.NewShellErrorWriteFile(msg)
77	case errors.FILE_OPEN:
78		return errors.NewShellErrorOpenFile(msg)
79	case errors.FILE_CLOSE:
80		return errors.NewShellErrorCloseFile(msg)
81
82	//Authentication Errors.
83	case errors.INVALID_PASSWORD:
84		return errors.NewShellErrorInvalidPassword(msg)
85	case errors.INVALID_USERNAME:
86		return errors.NewShellErrorInvalidUsername("")
87	case errors.MISSING_CREDENTIAL:
88		return errors.NewShellErrorMissingCredential("")
89	case errors.INVALID_CREDENTIAL:
90		return errors.NewShellErrorInvalidCredential("")
91
92	//Command Errors
93	case errors.NO_SUCH_COMMAND:
94		return errors.NewShellErrorNoSuchCommand(msg)
95	case errors.NO_SUCH_PARAM:
96		return errors.NewShellErrorNoSuchParam(msg)
97	case errors.TOO_MANY_ARGS:
98		return errors.NewShellErrorTooManyArgs("")
99	case errors.TOO_FEW_ARGS:
100		return errors.NewShellErrorTooFewArgs("")
101	case errors.STACK_EMPTY:
102		return errors.NewShellErrorStackEmpty("")
103	case errors.NO_SUCH_ALIAS:
104		return errors.NewShellErrorNoSuchAlias(msg)
105	case errors.BATCH_MODE:
106		return errors.NewShellErrorBatchMode("")
107
108	//Generic Errors
109	case errors.OPERATION_TIMEOUT:
110		return errors.NewShellErrorOperationTimeout(SERVICE_URL)
111	case errors.ROWS_SCAN:
112		return errors.NewShellErrorRowsScan(msg)
113	case errors.JSON_MARSHAL:
114		return errors.NewShellErrorJsonMarshal(msg)
115	case errors.JSON_UNMARSHAL:
116		return errors.NewShellErrorJsonUnmarshal(msg)
117	case errors.DRIVER_QUERY:
118		return errors.NewShellErrorDriverQueryMethod(msg)
119	case errors.WRITER_OUTPUT:
120		return errors.NewShellErrorWriterOutput(msg)
121	case errors.UNBALANCED_PAREN:
122		return errors.NewShellErrorUnbalancedParen("")
123	case errors.ROWS_CLOSE:
124		return errors.NewShellErrorRowsClose(msg)
125	case errors.CMD_LINE_ARG:
126		return errors.NewShellErrorCmdLineArgs("")
127	case errors.INVALID_INPUT_ARGUMENTS:
128		return errors.NewShellErrorInvalidInputArguments("")
129
130	default:
131		return errors.NewShellErrorUnkownError(msg)
132	}
133
134}
135
136/*
137	Function to print the error in Red.
138*/
139func PrintError(s_err errors.Error) {
140	tmpstr := fmt.Sprintln(fgRed, "ERROR", s_err.Code(), ":", s_err, reset)
141	io.WriteString(W, tmpstr+"\n")
142}
143