ladjs / superagent

Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
https://ladjs.github.io/superagent/
MIT License
16.59k stars 1.33k forks source link

How to show error of superagent request when I gor error on server? #1616

Open sergeynilov opened 3 years ago

sergeynilov commented 3 years ago

I added "superagent": "^6.1.0" into my Laravel 8 app and when I got error on Laravel side I try to catch it and show in flas message, like:

superagent .get('/profile/user_prop') // .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body // .set('X-API-Key', 'foobar') .set('accept', 'json') .end((err, res) => { console.log('err::') console.log(err)

    console.log('res::')
    console.log(res)
    if( res.error ) {
        console.log('res.error::')
        console.log(res.error)
        console.log('res.text::')
        console.log(res.text)
        popupErrorMessage( res.statusText + ( res.body != null ? ' : ' + res.body.message : '') )
    } else {
        $("#div_user_props").html(res.body.html);
    }
    // Calling the end function will send the request
});

It works ok, when I return error message in json return :

$userProfile     = Auth::user();
if ( empty($NonFoundUserProfile) ) {
    return response()->json(['message' => 'No Logged user !'], 500);
}

But in case of error in not in return block, like :

$userProps  = UserProp
    ::getByUYYserId($userProfile->id)  // NONEXISTING METHOD 
    ->orderBy('user_props.created_at', 'desc')
    ->get()
    ->map(function ($userPropItem) {
        return $userPropItem;
    })
    ->all();

I can not get error message and the only thing I found in browsers console : https://prnt.sc/xi7o13 and https://prnt.sc/xi7pnn the only place where I saw an error message is res.text : https://prnt.sc/xi7x1q but that is string literal of full error and if there is a way to get only this error text and flash it ?

Thanks!