Skip to content

OUT_MSG_DESCRIPTION

Jurek Muszyński edited this page Apr 17, 2019 · 9 revisions

void OUT_MSG_DESCRIPTION(int code)

Description

Writes error code, category and description to output buffer.

If MSG_FORMAT_JSON is not defined (default), it uses pipe-delimited format:

4|err|Page not found

If MSG_FORMAT_JSON is defined, it writes the following object:

{"code":4,"category":"err","message":"Page not found"}

Category may be used to use different colors for the messages. Categories are defined in silgy.h:

#define MSG_CAT_OK       "OK"
#define MSG_CAT_ERROR    "err"
#define MSG_CAT_WARNING  "war"
#define MSG_CAT_MESSAGE  "msg"

Returns

None

Example

/* in silgy_app_main */

int ret=OK;

if ( REQ("login") )
{
    gen_page_login(ci);
}
else if ( REQ("do_login") )    /* via AJAX */
{
    ret = silgy_usr_login(ci);
    OUT_MSG_DESCRIPTION(ret);
}

Client-side JavaScript:

let x = new XMLHttpRequest();

// "do_login" request...
// ...
// when ready:

if ( x.responseText.length > 0 )
{
    let r = x.responseText.split("|");

    if ( r[0] != "0" )  // error -- show message
    {
        show_msg(r[2]);
    }
    else    // ok -- redirect
    {
        window.location.href = "dashboard";
    }
}
else
{
    show_msg("No response");
}

JSON version:

let x = new XMLHttpRequest();

// "do_login" request...
// ...
// when ready:

if ( x.responseText.length > 0 )
{
    let r = JSON.parse(x.responseText);

    if ( r.code != 0 )  // error -- show message
    {
        show_msg(r.message);
    }
    else    // ok -- redirect
    {
        window.location.href = "dashboard";
    }
}
else
{
    show_msg("No response");
}
Clone this wiki locally