Graceful Degradation

Graceful Degradation

Hard C Error Handling 31 views
Explanation Complexity

Problem Statement

Create a program that loads data using the primary method. If it fails, create a fallback method. Multiple levels of error handling.

Input Format

Data source input (file / resource / configuration).

Output Format

Status message indicating whether data was loaded using:

• Primary method

• Fallback method

• Or failed completely

Example

Primary data source unavailable


Primary method failed. Loaded using fallback method.

Constraints

• Primary method may fail

• Fallback method may also fail

• Program must not crash

Concept Explanation

The program should try to load data using a main (primary) method first.
If it fails, it should gracefully switch to a fallback method.
Errors must be handled step by step.

Step-by-Step Explanation

1.Start the program.

2.Attempt to load data using the primary method.

3.If the primary method succeeds:

• Mark data as loaded.

• Continue program execution.

4.If the primary method fails:

• Log or display an error message.

5.Attempt to load data using the fallback method.

6.If the fallback method succeeds:

• Mark data as loaded.

• Continue program execution.

7.If the fallback method also fails:

• Log a critical error.

• Stop further processing safely.

8.Ensure every failure is handled without crashing the program.

Concept Explanation

The program should try to load data using a main (primary) method first.
If it fails, it should gracefully switch to a fallback method.
Errors must be handled step by step.

Step-by-Step Explanation

1.Start the program.

2.Attempt to load data using the primary method.

3.If the primary method succeeds:

• Mark data as loaded.

• Continue program execution.

4.If the primary method fails:

• Log or display an error message.

5.Attempt to load data using the fallback method.

6.If the fallback method succeeds:

• Mark data as loaded.

• Continue program execution.

7.If the fallback method also fails:

• Log a critical error.

• Stop further processing safely.

8.Ensure every failure is handled without crashing the program.

Input / Output Format

Input Format
Data source input (file / resource / configuration).
Output Format
Status message indicating whether data was loaded using:

• Primary method

• Fallback method

• Or failed completely
Constraints
• Primary method may fail

• Fallback method may also fail

• Program must not crash

Examples

Input:
Primary data source unavailable
Output:
Primary method failed. Loaded using fallback method.

Example Solution (Public)

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef enum {
    LOAD_SUCCESS = 0,
    LOAD_ERR_PRIMARY_FAILED = -1,
    LOAD_ERR_FALLBACK_FAILED = -2,
    LOAD_ERR_INVALID_INPUT = -3
} LoadStatus;

/* Primary load method (preferred source) */
LoadStatus load_primary(const char *path, char *buffer, size_t size)
{
    FILE *fp;

    if (!path || !buffer || size == 0) {
        return LOAD_ERR_INVALID_INPUT;
    }

    fp = fopen(path, "r");
    if (!fp) {
        return LOAD_ERR_PRIMARY_FAILED;
    }

    if (!fgets(buffer, size, fp)) {
        fclose(fp);
        return LOAD_ERR_PRIMARY_FAILED;
    }

    fclose(fp);
    return LOAD_SUCCESS;
}

/* Fallback load method (secondary source) */
LoadStatus load_fallback(char *buffer, size_t size)
{
    if (!buffer || size == 0) {
        return LOAD_ERR_INVALID_INPUT;
    }

    /* Example fallback: built-in default data */
    strncpy(buffer, "DEFAULT_DATA", size - 1);
    buffer[size - 1] = '�';

    return LOAD_SUCCESS;
}

/* High-level loader with multi-level error handling */
LoadStatus load_data(const char *primary_path, char *buffer, size_t size)
{
    LoadStatus status;

    /* 1. Try primary method */
    status = load_primary(primary_path, buffer, size);
    if (status == LOAD_SUCCESS) {
        return LOAD_SUCCESS;
    }

    fprintf(stderr, "Primary load failed, attempting fallback...n");

    /* 2. Try fallback method */
    status = load_fallback(buffer, size);
    if (status == LOAD_SUCCESS) {
        return LOAD_SUCCESS;
    }

    /* 3. All methods failed */
    return LOAD_ERR_FALLBACK_FAILED;
}

/* Example usage */
int main(void)
{
    char data[64];
    LoadStatus result;

    result = load_data("config.txt", data, sizeof(data));

    if (result == LOAD_SUCCESS) {
        printf("Loaded data: %sn", data);
    } else {
        printf("Data load failed, error code: %dn", result);
    }

    return 0;
}

Official Solution Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef enum {
    LOAD_SUCCESS = 0,
    LOAD_ERR_PRIMARY_FAILED = -1,
    LOAD_ERR_FALLBACK_FAILED = -2,
    LOAD_ERR_INVALID_INPUT = -3
} LoadStatus;

/* Primary load method (preferred source) */
LoadStatus load_primary(const char *path, char *buffer, size_t size)
{
    FILE *fp;

    if (!path || !buffer || size == 0) {
        return LOAD_ERR_INVALID_INPUT;
    }

    fp = fopen(path, "r");
    if (!fp) {
        return LOAD_ERR_PRIMARY_FAILED;
    }

    if (!fgets(buffer, size, fp)) {
        fclose(fp);
        return LOAD_ERR_PRIMARY_FAILED;
    }

    fclose(fp);
    return LOAD_SUCCESS;
}

/* Fallback load method (secondary source) */
LoadStatus load_fallback(char *buffer, size_t size)
{
    if (!buffer || size == 0) {
        return LOAD_ERR_INVALID_INPUT;
    }

    /* Example fallback: built-in default data */
    strncpy(buffer, "DEFAULT_DATA", size - 1);
    buffer[size - 1] = '�';

    return LOAD_SUCCESS;
}

/* High-level loader with multi-level error handling */
LoadStatus load_data(const char *primary_path, char *buffer, size_t size)
{
    LoadStatus status;

    /* 1. Try primary method */
    status = load_primary(primary_path, buffer, size);
    if (status == LOAD_SUCCESS) {
        return LOAD_SUCCESS;
    }

    fprintf(stderr, "Primary load failed, attempting fallback...n");

    /* 2. Try fallback method */
    status = load_fallback(buffer, size);
    if (status == LOAD_SUCCESS) {
        return LOAD_SUCCESS;
    }

    /* 3. All methods failed */
    return LOAD_ERR_FALLBACK_FAILED;
}

/* Example usage */
int main(void)
{
    char data[64];
    LoadStatus result;

    result = load_data("config.txt", data, sizeof(data));

    if (result == LOAD_SUCCESS) {
        printf("Loaded data: %sn", data);
    } else {
        printf("Data load failed, error code: %dn", result);
    }

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.