Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to throw error correctly in actor method? #452

Open
raymondsze opened this issue Sep 18, 2023 · 1 comment
Open

How to throw error correctly in actor method? #452

raymondsze opened this issue Sep 18, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@raymondsze
Copy link

I have an actor within following method

func (t *TestActor) Invoke(ctx context.Context, req string) (string, error) {
	return "", errors.New("Custom Message")
}

And I invoke the actor like this

	ctx, _ = context.WithTimeout(ctx, time.Second*15)
	rsp, err := myActor.Invoke(ctx, "laurence")
	if err != nil {
		panic(err)
	}
	fmt.Println("get invoke result = ", rsp)

It should always go panic(err), but the error shown is always "panic: error invoking binding testActorType/ActorImplID5121f5e3-bb26-4362-a199-e20ec9ebd5d5: rpc error: code = Internal desc = error invoke actor method: rpc error: code = Internal desc = error invoke actor method: error from actor service: "

Please advise..., also how to customize the grpc error code? now its always "Internal".

@raymondsze raymondsze added the bug Something isn't working label Sep 18, 2023
@qazwsxedckll
Copy link

qazwsxedckll commented Dec 14, 2023

// InvokeMethod to invoke local function by @actorID, @methodName and @request request param.
func (m *DefaultActorManagerContext) InvokeMethod(ctx context.Context, actorID, methodName string, request []byte) ([]byte, actorErr.ActorErr) {
	if m.factory == nil {
		return nil, actorErr.ErrActorFactoryNotSet
	}

	actorContainer, aerr := m.getAndCreateActorContainerIfNotExist(ctx, actorID)
	if aerr != actorErr.Success {
		return nil, aerr
	}
	returnValue, aerr := actorContainer.Invoke(ctx, methodName, request)
	if aerr != actorErr.Success {
		return nil, aerr
	}
	if len(returnValue) == 1 {
		return nil, actorErr.Success
	}

	var (
		retErr interface{}
		replyv reflect.Value
	)

	if len(returnValue) == 2 {
		replyv = returnValue[0]
		retErr = returnValue[1].Interface()
	}

	if retErr != nil {
		return nil, actorErr.ErrActorInvokeFailed
	}
	rspData, err := m.serializer.Marshal(replyv.Interface())
	if err != nil {
		return nil, actorErr.ErrActorMethodSerializeFailed
	}
	if err := actorContainer.GetActor().SaveState(ctx); err != nil {
		return nil, actorErr.ErrSaveStateFailed
	}
	return rspData, actorErr.Success
}

While investigating your problem, I also find that the response such as error is ignored when len(returnValue) == 1 and SaveState(ctx) is not invoked.

Any response from the contributer?

@LaurenceLiZhixin

Update:

Your problem comes from

	if retErr != nil {
		return nil, actorErr.ErrActorInvokeFailed
	}

the business error get thrown here also.

	// register actor method invoke handler
	fInvoke := func(w http.ResponseWriter, r *http.Request) {
		actorType := chi.URLParam(r, "actorType")
		actorID := chi.URLParam(r, "actorId")
		methodName := chi.URLParam(r, "methodName")
		reqData, _ := io.ReadAll(r.Body)
		rspData, err := runtime.GetActorRuntimeInstanceContext().InvokeActorMethod(r.Context(), actorType, actorID, methodName, reqData)
		if err == actorErr.ErrActorTypeNotFound {
			w.WriteHeader(http.StatusNotFound)
			return
		}
		if err != actorErr.Success {
			w.WriteHeader(http.StatusInternalServerError)    // <--------here
			return
		}
		w.WriteHeader(http.StatusOK)
		_, _ = w.Write(rspData)
	}
	s.mux.Put("/actors/{actorType}/{actorId}/method/{methodName}", fInvoke)

when actorErr is not Success, the body is empty.

	if resp.Status().GetCode() != http.StatusOK {
		respData, _ := resp.RawDataFull()
		return nil, fmt.Errorf("error from actor service: %s", string(respData))
	}

In dapr runtime, the message is printed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants