1

I have this simple class :

import {Router} from '@angular/router';

export class NotLogged {
    constructor(error, private router: Router) {
        alert('Not logged in !');
        this.router.navigate(['/']);
    }
}

And then I'm trying to call it from a service :

if(error.status === 401)
    return Observable.throw(new NotLogged(error.json()));

Of course I get an error Supplied parameters do not match any signature of call target. How can I do that ?

Thanks ahead.

2
  • 1
    It looks like it is supposed to be class NotLogged extends Error. And this would be error itself. Class design went wrong. router.navigate returns a promise and it shouldn't be lost because it may be useful for control flow. Obviously, a promise cannot be returned from error class either. Commented Oct 19, 2017 at 18:15
  • 3 of the answers are right, I selected the most clear for me, and upvoted all 3. Thanks. Commented Oct 19, 2017 at 19:02

4 Answers 4

1

The class doesn't inherit from another one and isn't supposed to have super. If it is supposed to be Error subclass, router instance should be passed from a service where the class is instantiated:

new NotLogged(error.json(), router)

Usually classes that are supposed to make use of Angular DI shouldn't be constructed manually. In this case it can be existing class instance that will be used in the application, not a class itself. Also, a promise that router.navigate returns is valuable and shouldn't be discarded:

@Injectable()
export class NotLogged {
    constructor(private router: Router) {}

    notLogged(error) {
        alert('Not logged in !');
        return this.router.navigate(['/']);
    }
}

At this point it becomes obvious that class design went wrong, and notLogged should belong to not to just some class but to a service that does something besides logging out, e.g. Auth.

Sign up to request clarification or add additional context in comments.

Comments

1

constructor should be,

constructor(private error : any, private router: Router) {

1 Comment

Thanks, you're right, but it doesn't get rid of the compilation error
1

I'd add router as a dependency to the service and inject it manually.

But the usage of your class is a bit strange, why would you redirect in the constructor of a data/error class. Maybe the better solution is to remove the router from the class and check for this error class in the consumer of the service (probably a component) and redirect when needed. This also helps with separating your view logic (redirecting) from your business logic

Comments

1

I'm not clear why you call super(); in a nonderived class. For the question, you should use a different method to get the object from your class.

export class NotLogged {
    constructor(private router: Router) {

    }

    proceedWithError(error:any){           
        alert('Not logged in !');
        this.router.navigate(['/']);
        return this;
    }
}

then you can use that method to do the needful.

if(error.status === 401)
    return Observable.throw(new NotLogged().proceedWithError(error.json());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.