Exemple #1
0
func (this *IndexConnection) Error(err errors.Error) {
	if this.primary && err.Code() == errors.INDEX_SCAN_TIMEOUT {
		this.timeout = true
		return
	}
	this.context.Error(err)
}
Exemple #2
0
func mapErrorToHttpStatus(err errors.Error) int {
	switch err.Code() {
	case errors.ADMIN_AUTH_ERROR:
		return http.StatusUnauthorized
	case errors.ADMIN_SSL_NOT_ENABLED:
		return http.StatusNotFound
	default:
		return http.StatusInternalServerError
	}
}
Exemple #3
0
func mapErrorToHttpResponse(err errors.Error) int {
	switch err.Code() {
	case 1000: // readonly violation
		return http.StatusForbidden
	case 1010: // unsupported http method
		return http.StatusMethodNotAllowed
	case 1020, 1030, 1040, 1050, 1060, 1065, 1070:
		return http.StatusBadRequest
	case 1120:
		return http.StatusNotAcceptable
	case 3000: // parse error range
		return http.StatusBadRequest
	case 4000, errors.NO_SUCH_PREPARED: // plan error range
		return http.StatusNotFound
	case 5000:
		return http.StatusInternalServerError
	default:
		return http.StatusInternalServerError
	}
}
Exemple #4
0
func (this *httpRequest) writeError(err errors.Error, count int) bool {
	var rv bool
	if count == 0 {
		rv = this.writeString("\n")
	} else {
		rv = this.writeString(",\n")
	}

	m := map[string]interface{}{
		"code": err.Code(),
		"msg":  err.Error(),
	}
	bytes, er := json.MarshalIndent(m, "        ", "    ")
	if er != nil {
		return false
	}

	return rv &&
		this.writeString("        ") &&
		this.writeString(string(bytes))
}
Exemple #5
0
/*
	Function to print the error in Red.
*/
func PrintError(s_err errors.Error) {
	tmpstr := fmt.Sprintln(fgRed, "ERROR", s_err.Code(), ":", s_err, reset)
	io.WriteString(W, tmpstr+"\n")
}
Exemple #6
0
func newHttpRequest(resp http.ResponseWriter, req *http.Request, bp BufferPool, size int) *httpRequest {
	var httpArgs httpRequestArgs
	var err errors.Error

	// Limit body size in case of denial-of-service attack
	req.Body = http.MaxBytesReader(resp, req.Body, int64(size))

	e := req.ParseForm()
	if e != nil {
		err = errors.NewServiceErrorBadValue(e, "request form")
	}

	if err != nil && req.Method != "GET" && req.Method != "POST" {
		err = errors.NewServiceErrorHTTPMethod(req.Method)
	}

	err = contentNegotiation(resp, req)

	if err == nil {
		httpArgs, err = getRequestParams(req)
	}

	var statement string
	if err == nil {
		statement, err = httpArgs.getStatement()
	}

	var prepared *plan.Prepared

	if err == nil {
		prepared, err = getPrepared(httpArgs)
		plan, plan_err := getEncodedPlan(httpArgs)
		if err != nil && err.Code() == errors.NO_SUCH_PREPARED {
			if plan_err != nil {
				err = plan_err
			}
			if plan_err == nil && plan != nil {
				prepared = plan
				err = nil
			}
		}
		if err == nil && plan_err != nil {
			err = plan_err
		}
		if prepared != nil && plan != nil && prepared.EncodedPlan() != plan.EncodedPlan() {
			err = errors.NewPreparedEncodingMismatchError(prepared.Name())
		}
	}

	if err == nil && statement == "" && prepared == nil {
		err = errors.NewServiceErrorMissingValue("statement or prepared")
	}

	var namedArgs map[string]value.Value
	if err == nil {
		namedArgs, err = httpArgs.getNamedArgs()
	}

	var positionalArgs value.Values
	if err == nil {
		positionalArgs, err = httpArgs.getPositionalArgs()
	}

	var namespace string
	if err == nil {
		namespace, err = httpArgs.getString(NAMESPACE, "")
	}

	var timeout time.Duration
	if err == nil {
		timeout, err = httpArgs.getDuration(TIMEOUT)
	}

	var max_parallelism int
	if err == nil {
		var maxp string
		maxp, err = httpArgs.getString(MAX_PARALLELISM, "")
		if err == nil && maxp != "" {
			var e error
			max_parallelism, e = strconv.Atoi(maxp)
			if e != nil {
				err = errors.NewServiceErrorBadValue(e, "max parallelism")
			}
		}
	}

	var readonly value.Tristate
	if err == nil {
		readonly, err = getReadonly(httpArgs, req.Method == "GET")
	}

	var metrics value.Tristate
	if err == nil {
		metrics, err = httpArgs.getTristate(METRICS)
	}

	var format Format
	if err == nil {
		format, err = getFormat(httpArgs)
	}

	if err == nil && format != JSON {
		err = errors.NewServiceErrorNotImplemented("format", format.String())
	}

	var signature value.Tristate
	if err == nil {
		signature, err = httpArgs.getTristate(SIGNATURE)
	}

	var compression Compression
	if err == nil {
		compression, err = getCompression(httpArgs)
	}

	if err == nil && compression != NONE {
		err = errors.NewServiceErrorNotImplemented("compression", compression.String())
	}

	var encoding Encoding
	if err == nil {
		encoding, err = getEncoding(httpArgs)
	}

	if err == nil && encoding != UTF8 {
		err = errors.NewServiceErrorNotImplemented("encoding", encoding.String())
	}

	var pretty value.Tristate
	if err == nil {
		pretty, err = httpArgs.getTristate(PRETTY)
	}

	if err == nil && pretty == value.FALSE {
		err = errors.NewServiceErrorNotImplemented("pretty", "false")
	}

	var consistency *scanConfigImpl

	if err == nil {
		consistency, err = getScanConfiguration(httpArgs)
	}

	var creds datastore.Credentials
	if err == nil {
		creds, err = getCredentials(httpArgs, req.Header["Authorization"])
	}

	client_id := ""
	if err == nil {
		client_id, err = getClientID(httpArgs)
	}

	base := server.NewBaseRequest(statement, prepared, namedArgs, positionalArgs, namespace,
		max_parallelism, readonly, metrics, signature, consistency, client_id, creds)

	rv := &httpRequest{
		BaseRequest:   *base,
		resp:          resp,
		req:           req,
		requestNotify: make(chan bool, 1),
	}

	rv.SetTimeout(rv, timeout)

	rv.writer = NewBufferedWriter(rv, bp)

	// Abort if client closes connection; alternatively, return when request completes.
	closeNotify := resp.(http.CloseNotifier).CloseNotify()
	closeNotifier := func() {
		select {
		case <-closeNotify:
			rv.Expire()
			return
		case <-rv.requestNotify:
			return
		}
	}
	go closeNotifier()

	if err != nil {
		rv.Fail(err)
	}

	return rv
}