Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
errors.c
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <[email protected]>
4 *
5 * This library is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#include "serene/rt/errors.h"
20
21#if defined(__unix__) || defined(__APPLE__)
22# include "errno.h"
23#endif
24#include "serene/rt/context.h"
25#include "serene/utils.h"
26
27#ifdef SRN_WITH_SERENE
28# include "serene/rt/core.h"
29#endif
30
31srn_error_t *srn_errors_make(const srn_context_t *ctx, srn_error_tag_t tag, const char *msg) {
32 srn_error_t *err = ALLOC(ctx, srn_error_t);
33 PANIC_IF_NULL(err);
34 err->tag = tag;
35 err->msg = (char *)msg;
36 return err;
37}
38
40 const srn_context_t *ctx, size_t size, size_t alignment, srn_error_tag_t tag, const char *msg
41) {
42 // The ERROR_HEADER convention guarantees `srn_error_t *maybe_error` is the
43 // first member of any fallible T, so offset 0 of the value is that pointer.
44 // Allocate the value and set it to a fresh error; the caller checks it with
45 // HAS_ERROR and never reads the other fields of an errored value.
46 void *value = srn_allocate(ctx, size, alignment);
47 PANIC_IF_NULL(value);
48 *(srn_error_t **)value = srn_errors_make(ctx, tag, msg);
49 return value;
50}
51
52#if defined(__unix__) || defined(__APPLE__)
53srn_error_tag_t srn_errno_to_tag(int errno_value, srn_error_tag_t default_tag) {
54 switch (errno_value) {
55 case ECONNREFUSED:
56 return CONNECTION_REFUSED;
57 case ECONNRESET:
58 return CONNECTION_RESET;
59 case ECONNABORTED:
60 return CONNECTION_ABORTED;
61 case EPIPE:
62 return BROKEN_PIPE;
63 case EBADF:
64 return BAD_DESCRIPTOR;
65 case ETIMEDOUT:
66 return TIMED_OUT;
67 case ECANCELED:
68 return CANCELED;
69 case ENOTCONN:
70 return NOT_CONNECTED;
71 case EADDRINUSE:
72 return ADDRESS_IN_USE;
73 case EHOSTUNREACH:
74 return HOST_UNREACHABLE;
75 case ENETUNREACH:
77 case EMSGSIZE:
78 return MESSAGE_TOO_LONG;
79 case EMFILE:
80 case ENFILE:
82 case EACCES:
83 case EPERM:
84 return PERMISSION_DENIED;
85 case ENOMEM:
86 case ENOBUFS:
87 return OUT_OF_MEMORY;
88 case ENOENT:
89 return NOT_FOUND;
90 case EEXIST:
91 return ALREADY_EXISTS;
92 case EISDIR:
93 return IS_A_DIRECTORY;
94 case ENOTDIR:
95 return NOT_A_DIRECTORY;
96 case ENOSPC:
97 return NO_SPACE;
98 default:
99 return default_tag;
100 }
101}
102#else
103# error "srn_errno_to_tag has no errno mapping for this target; provide a native mapping."
104#endif
105
106#ifdef SRN_WITH_SERENE
107srn_value_t *srn_errors_make_error(
108 srn_context_t *ctx, srn_metadata_t *metadata, srn_error_tag_t tag, const char *msg
109) {
110 PANIC_IF_NULL(metadata);
111 srn_error_t *err = srn_errors_make(ctx, tag, msg);
112 return srn_value_make(ctx, VError, metadata, (void *)err);
113}
114
116srn_errors_err_to_value(srn_context_t *ctx, srn_metadata_t *metadata, srn_error_t *err) {
117 PANIC_IF_NULL(err);
118 PANIC_IF_NULL(ctx);
119 return srn_value_make(ctx, VError, metadata, (void *)err);
120}
121#endif
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition context.c:73
#define ALLOC(ctx, T)
Definition context.h:84
srn_value_t * srn_value_make(srn_context_t *ctx, srn_value_tag_t tag, srn_metadata_t *metadata, void *payload)
Creates a new serene value.
Definition core.c:36
@ VError
VError should be last.
Definition core.h:128
void * srn_errors_fail(const srn_context_t *ctx, size_t size, size_t alignment, srn_error_tag_t tag, const char *msg)
Allocate a size/alignment value in ctx, set its ERROR_HEADER (the first member, srn_error_t *maybe_er...
Definition errors.c:39
srn_error_t * srn_errors_make(const srn_context_t *ctx, srn_error_tag_t tag, const char *msg)
Build an error in ctx's memory, tagged tag and carrying msg.
Definition errors.c:31
Error handling for the runtime.
srn_error_tag_t
The kind of a failure.
Definition errors.h:75
@ BROKEN_PIPE
Definition errors.h:94
@ HOST_UNREACHABLE
Definition errors.h:100
@ NETWORK_UNREACHABLE
Definition errors.h:101
@ IS_A_DIRECTORY
Definition errors.h:108
@ PERMISSION_DENIED
Definition errors.h:104
@ CONNECTION_RESET
Definition errors.h:92
@ TOO_MANY_OPEN_FILES
Definition errors.h:103
@ NOT_CONNECTED
Definition errors.h:98
@ CONNECTION_REFUSED
Definition errors.h:91
@ CONNECTION_ABORTED
Definition errors.h:93
@ ADDRESS_IN_USE
Definition errors.h:99
@ BAD_DESCRIPTOR
Definition errors.h:95
@ OUT_OF_MEMORY
Definition errors.h:105
@ NOT_FOUND
Definition errors.h:106
@ CANCELED
Definition errors.h:97
@ NOT_A_DIRECTORY
Definition errors.h:109
@ TIMED_OUT
Definition errors.h:96
@ MESSAGE_TOO_LONG
Definition errors.h:102
@ ALREADY_EXISTS
Definition errors.h:107
@ NO_SPACE
Definition errors.h:110
srn_error_tag_t srn_errno_to_tag(int errno_value, srn_error_tag_t default_tag)
Map a POSIX errno to error tag.
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
char * msg
Definition errors.h:127
srn_error_tag_t tag
Definition errors.h:126
#define PANIC_IF_NULL(ptr)
Definition utils.h:66