Ejemplo n.º 1
0
// ReadErrorResponse reads an "Exception" message from the OrientDB server.
// The OrientDB server can return multiple exceptions, all of which are
// incorporated into a single OServerException Error struct.
// If error (the second return arg) is not nil, then there was a
// problem reading the server exception on the wire.
func readErrorResponse(r *rw.Reader) (serverException error) {
	var (
		exClass, exMsg string
	)
	exc := make([]orient.Exception, 0, 1) // usually only one ?
	for {
		// before class/message combo there is a 1 (continue) or 0 (no more)
		marker := r.ReadByte()
		if marker == byte(0) {
			break
		}
		exClass = r.ReadString()
		exMsg = r.ReadString()
		exc = append(exc, orient.UnknownException{Class: exClass, Message: exMsg})
	}

	// Next there *may* a serialized exception of bytes, but it is only
	// useful to Java clients, so read and ignore if present.
	// If there is no serialized exception, EOF will be returned
	_ = r.ReadBytes()

	for _, e := range exc {
		switch e.ExcClass() {
		case "com.orientechnologies.orient.core.storage.ORecordDuplicatedException":
			return ODuplicatedRecordException{OServerException: orient.OServerException{Exceptions: exc}}
		}
	}
	return orient.OServerException{Exceptions: exc}
}
Ejemplo n.º 2
0
func (db *Database) readIdentifiable(r *rw.Reader) (orient.OIdentifiable, error) {
	classId := r.ReadShort()
	if err := r.Err(); err != nil {
		return nil, err
	}
	switch classId {
	case RecordNull:
		return nil, nil
	case RecordRID:
		var rid orient.RID
		if err := rid.FromStream(r); err != nil {
			return nil, err
		}
		return rid, nil
	default:
		tp := orient.RecordType(r.ReadByte())
		if err := r.Err(); err != nil {
			return nil, err
		}
		record := orient.NewRecordOfType(tp)
		switch rec := record.(type) {
		case *orient.Document:
			rec.SetSerializer(db.sess.cli.recordFormat)
		}

		var rid orient.RID
		if err := rid.FromStream(r); err != nil {
			return nil, err
		}
		version := int(r.ReadInt())
		content := r.ReadBytes()

		if err := record.Fill(rid, version, content); err != nil {
			return nil, fmt.Errorf("cannot create record %T from content: %s", record, err)
		}
		return record, nil
	}
}